comparison kdiff3/src-QT4/kdiff3_part.cpp @ 80:fcd146072e0c

0.9.93
author joachim99
date Tue, 06 Jan 2009 17:51:29 +0000
parents 1184fc843210
children
comparison
equal deleted inserted replaced
79:881e47f5076b 80:fcd146072e0c
17 * 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. * 17 * 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/ 18 ***************************************************************************/
19 19
20 #include "kdiff3_part.h" 20 #include "kdiff3_part.h"
21 21
22 #include <kinstance.h> 22 #include <kcomponentdata.h>
23 #include <kaction.h> 23 #include <kaction.h>
24 #include <kstdaction.h> 24 #include <kstandardaction.h>
25 #include <kfiledialog.h> 25 #include <kfiledialog.h>
26 26
27 #include <qfile.h> 27 #include <qfile.h>
28 #include <qtextstream.h> 28 #include <qtextstream.h>
29 #include <QProcess> 29 #include <QProcess>
36 #include <iostream> 36 #include <iostream>
37 37
38 #include "version.h" 38 #include "version.h"
39 39
40 KDiff3Part::KDiff3Part( QWidget *parentWidget, const char *widgetName, 40 KDiff3Part::KDiff3Part( QWidget *parentWidget, const char *widgetName,
41 QObject *parent, const char *name ) 41 QObject *parent )
42 : KParts::ReadOnlyPart(parent, name) 42 : KParts::ReadWritePart(parent)
43 { 43 {
44 // we need an instance 44 // we need an instance
45 setInstance( KDiff3PartFactory::instance() ); 45 setComponentData( *KDiff3PartFactory::instance() );
46 46
47 // this should be your custom internal widget 47 // this should be your custom internal widget
48 m_widget = new KDiff3App( parentWidget, widgetName, this ); 48 m_widget = new KDiff3App( parentWidget, widgetName, this );
49 49
50 // This hack is necessary to avoid a crash when the program terminates. 50 // This hack is necessary to avoid a crash when the program terminates.
52 52
53 // notify the part that this is our internal widget 53 // notify the part that this is our internal widget
54 setWidget(m_widget); 54 setWidget(m_widget);
55 55
56 // create our actions 56 // create our actions
57 //KStdAction::open(this, SLOT(fileOpen()), actionCollection()); 57 //KStandardAction::open(this, SLOT(fileOpen()), actionCollection());
58 //KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection()); 58 //KStandardAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
59 //KStdAction::save(this, SLOT(save()), actionCollection()); 59 //KStandardAction::save(this, SLOT(save()), actionCollection());
60 60
61 setXMLFile("kdiff3_part.rc"); 61 setXMLFile("kdiff3_part.rc");
62 62
63 // we are read-write by default 63 // we are read-write by default
64 setReadWrite(true); 64 setReadWrite(true);
69 69
70 KDiff3Part::~KDiff3Part() 70 KDiff3Part::~KDiff3Part()
71 { 71 {
72 if ( m_widget!=0 && ! m_bIsShell ) 72 if ( m_widget!=0 && ! m_bIsShell )
73 { 73 {
74 m_widget->saveOptions( m_widget->isPart() ? instance()->config() : kapp->config() ); 74 m_widget->saveOptions( m_widget->isPart() ? componentData().config() : KGlobal::config() );
75 } 75 }
76 } 76 }
77 77
78 void KDiff3Part::setReadWrite(bool /*rw*/) 78 void KDiff3Part::setReadWrite(bool /*rw*/)
79 { 79 {
82 82
83 void KDiff3Part::setModified(bool /*modified*/) 83 void KDiff3Part::setModified(bool /*modified*/)
84 { 84 {
85 /* 85 /*
86 // get a handle on our Save action and make sure it is valid 86 // get a handle on our Save action and make sure it is valid
87 KAction *save = actionCollection()->action(KStdAction::stdName(KStdAction::Save)); 87 KAction *save = actionCollection()->action(KStandardAction::stdName(KStandardAction::Save));
88 if (!save) 88 if (!save)
89 return; 89 return;
90 90
91 // if so, we either enable or disable it based on the current 91 // if so, we either enable or disable it based on the current
92 // state 92 // state
128 128
129 129
130 bool KDiff3Part::openFile() 130 bool KDiff3Part::openFile()
131 { 131 {
132 // m_file is always local so we can use QFile on it 132 // m_file is always local so we can use QFile on it
133 std::cerr << "KDiff3: " << m_file.toLatin1().constData() << std::endl; 133 std::cerr << "KDiff3: " << localFilePath().toLatin1().constData() << std::endl;
134 QFile file(m_file); 134 QFile file(localFilePath());
135 if (file.open(QIODevice::ReadOnly) == false) 135 if (file.open(QIODevice::ReadOnly) == false)
136 return false; 136 return false;
137 137
138 // our example widget is text-based, so we use QTextStream instead 138 // our example widget is text-based, so we use QTextStream instead
139 // of a raw QDataStream 139 // of a raw QDataStream
169 else if ( version1.isEmpty() && f1.exists() ) 169 else if ( version1.isEmpty() && f1.exists() )
170 { 170 {
171 // Normal patch 171 // Normal patch
172 // patch -f -u --ignore-whitespace -i [inputfile] -o [outfile] [patchfile] 172 // patch -f -u --ignore-whitespace -i [inputfile] -o [outfile] [patchfile]
173 QString tempFileName = FileAccess::tempFileName(); 173 QString tempFileName = FileAccess::tempFileName();
174 QString cmd = "patch -f -u --ignore-whitespace -i \"" + m_file + 174 QString cmd = "patch -f -u --ignore-whitespace -i \"" + localFilePath() +
175 "\" -o \""+tempFileName + "\" \"" + fileName1+ "\""; 175 "\" -o \""+tempFileName + "\" \"" + fileName1+ "\"";
176 176
177 QProcess process; 177 QProcess process;
178 process.start( cmd ); 178 process.start( cmd );
179 process.waitForFinished(-1); 179 process.waitForFinished(-1);
180 180
181 m_widget->slotFileOpen2( fileName1, tempFileName, "", "", 181 m_widget->slotFileOpen2( fileName1, tempFileName, "", "",
182 "", version2.isEmpty() ? fileName2 : "REV:"+version2+":"+fileName2, "", 0 ); // alias names 182 "", version2.isEmpty() ? fileName2 : "REV:"+version2+":"+fileName2, "", 0 ); // alias names
183 // std::cerr << "KDiff3: f1:" << fileName1.latin1() <<"<->"<<tempFileName.latin1()<< std::endl; 183 // std::cerr << "KDiff3: f1:" << fileName1.toLatin1() <<"<->"<<tempFileName.toLatin1()<< std::endl;
184 FileAccess::removeTempFile( tempFileName ); 184 FileAccess::removeTempFile( tempFileName );
185 } 185 }
186 else if ( version2.isEmpty() && f2.exists() ) 186 else if ( version2.isEmpty() && f2.exists() )
187 { 187 {
188 // Reverse patch 188 // Reverse patch
189 // patch -f -u -R --ignore-whitespace -i [inputfile] -o [outfile] [patchfile] 189 // patch -f -u -R --ignore-whitespace -i [inputfile] -o [outfile] [patchfile]
190 QString tempFileName = FileAccess::tempFileName(); 190 QString tempFileName = FileAccess::tempFileName();
191 QString cmd = "patch -f -u -R --ignore-whitespace -i \"" + m_file + 191 QString cmd = "patch -f -u -R --ignore-whitespace -i \"" + localFilePath() +
192 "\" -o \""+tempFileName + "\" \"" + fileName2+"\""; 192 "\" -o \""+tempFileName + "\" \"" + fileName2+"\"";
193 193
194 QProcess process; 194 QProcess process;
195 process.start( cmd ); 195 process.start( cmd );
196 process.waitForFinished(-1); 196 process.waitForFinished(-1);
197 197
198 m_widget->slotFileOpen2( tempFileName, fileName2, "", "", 198 m_widget->slotFileOpen2( tempFileName, fileName2, "", "",
199 version1.isEmpty() ? fileName1 : "REV:"+version1+":"+fileName1, "", "", 0 ); // alias name 199 version1.isEmpty() ? fileName1 : "REV:"+version1+":"+fileName1, "", "", 0 ); // alias name
200 // std::cerr << "KDiff3: f2:" << fileName2.latin1() <<"<->"<<tempFileName.latin1()<< std::endl; 200 // std::cerr << "KDiff3: f2:" << fileName2.toLatin1() <<"<->"<<tempFileName.toLatin1()<< std::endl;
201 FileAccess::removeTempFile( tempFileName ); 201 FileAccess::removeTempFile( tempFileName );
202 } 202 }
203 else if ( !version1.isEmpty() && !version2.isEmpty() ) 203 else if ( !version1.isEmpty() && !version2.isEmpty() )
204 { 204 {
205 std::cerr << "KDiff3: f1/2:" << fileName1.toLatin1().constData() <<"<->"<<fileName2.toLatin1().constData()<< std::endl; 205 std::cerr << "KDiff3: f1/2:" << fileName1.toLatin1().constData() <<"<->"<<fileName2.toLatin1().constData()<< std::endl;
222 "REV:"+version1+":"+fileName1, 222 "REV:"+version1+":"+fileName1,
223 "REV:"+version2+":"+fileName2, 223 "REV:"+version2+":"+fileName2,
224 "", 0 224 "", 0
225 ); 225 );
226 226
227 // std::cerr << "KDiff3: f1/2:" << tempFileName1.latin1() <<"<->"<<tempFileName2.latin1()<< std::endl; 227 // std::cerr << "KDiff3: f1/2:" << tempFileName1.toLatin1() <<"<->"<<tempFileName2.toLatin1()<< std::endl;
228 FileAccess::removeTempFile( tempFileName1 ); 228 FileAccess::removeTempFile( tempFileName1 );
229 FileAccess::removeTempFile( tempFileName2 ); 229 FileAccess::removeTempFile( tempFileName2 );
230 return true; 230 return true;
231 } 231 }
232 else 232 else
241 { 241 {
242 /* // if we aren't read-write, return immediately 242 /* // if we aren't read-write, return immediately
243 if (isReadWrite() == false) 243 if (isReadWrite() == false)
244 return false; 244 return false;
245 245
246 // m_file is always local, so we use QFile 246 // localFilePath() is always local, so we use QFile
247 QFile file(m_file); 247 QFile file(localFilePath());
248 if (file.open(IO_WriteOnly) == false) 248 if (file.open(IO_WriteOnly) == false)
249 return false; 249 return false;
250 250
251 // use QTextStream to dump the text to the file 251 // use QTextStream to dump the text to the file
252 QTextStream stream(&file); 252 QTextStream stream(&file);
261 261
262 // It's usually safe to leave the factory code alone.. with the 262 // It's usually safe to leave the factory code alone.. with the
263 // notable exception of the KAboutData data 263 // notable exception of the KAboutData data
264 #include <kaboutdata.h> 264 #include <kaboutdata.h>
265 #include <klocale.h> 265 #include <klocale.h>
266 266 #include <kglobal.h>
267 KInstance* KDiff3PartFactory::s_instance = 0L; 267
268 KComponentData* KDiff3PartFactory::s_instance = 0L;
268 KAboutData* KDiff3PartFactory::s_about = 0L; 269 KAboutData* KDiff3PartFactory::s_about = 0L;
269 270
270 KDiff3PartFactory::KDiff3PartFactory() 271 KDiff3PartFactory::KDiff3PartFactory()
271 : KParts::Factory() 272 : KParts::Factory()
272 { 273 {
278 delete s_about; 279 delete s_about;
279 280
280 s_instance = 0L; 281 s_instance = 0L;
281 } 282 }
282 283
283 KParts::Part* KDiff3PartFactory::createPartObject( QWidget *parentWidget, const char *widgetName, 284 KParts::Part* KDiff3PartFactory::createPartObject( QWidget *parentWidget,
284 QObject *parent, const char *name, 285 QObject *parent,
285 const char *classname, const QStringList&/*args*/ ) 286 const char *classname, const QStringList&/*args*/ )
286 { 287 {
287 // Create an instance of our Part 288 // Create an instance of our Part
288 KDiff3Part* obj = new KDiff3Part( parentWidget, widgetName, parent, name ); 289 KDiff3Part* obj = new KDiff3Part( parentWidget, 0, parent );
289 290
290 // See if we are to be read-write or not 291 // See if we are to be read-write or not
291 if (QString(classname) == "KParts::ReadOnlyPart") 292 if (QString(classname) == "KParts::ReadOnlyPart")
292 obj->setReadWrite(false); 293 obj->setReadWrite(false);
293 294
294 return obj; 295 return obj;
295 } 296 }
296 297
297 KInstance* KDiff3PartFactory::instance() 298 KComponentData* KDiff3PartFactory::instance()
298 { 299 {
299 if( !s_instance ) 300 if( !s_instance )
300 { 301 {
301 s_about = new KAboutData("kdiff3part", I18N_NOOP("KDiff3Part"), VERSION); 302 s_about = new KAboutData(QByteArray("kdiff3part"), QByteArray("kdiff3part"), ki18n("KDiff3Part"), QByteArray(VERSION));
302 s_about->addAuthor("Joachim Eibl", 0, "joachim.eibl at gmx.de"); 303 s_about->addAuthor(ki18n("Joachim Eibl"), KLocalizedString(), QByteArray("joachim.eibl at gmx.de"));
303 s_instance = new KInstance(s_about); 304 s_instance = new KComponentData(s_about);
304 } 305 }
305 return s_instance; 306 return s_instance;
306 } 307 }
307 308
308 extern "C" 309 extern "C"