comparison kdiff3/src/kdiff3_part.cpp @ 8:86d21651c8db

KDiff3 version 0.9.70
author joachim99
date Mon, 06 Oct 2003 18:50:45 +0000
parents
children 32d5cbf9db71
comparison
equal deleted inserted replaced
7:ff98a43bbfea 8:86d21651c8db
1 /*
2 * Copyright (C) 2003 Joachim Eibl <joachim.eibl@gmx.de>
3 */
4
5 #include "kdiff3_part.h"
6
7 #include <kinstance.h>
8 #include <kaction.h>
9 #include <kstdaction.h>
10 #include <kfiledialog.h>
11
12 #include <qfile.h>
13 #include <qtextstream.h>
14 #include "kdiff3.h"
15 #include "fileaccess.h"
16
17 #include <kmessagebox.h>
18 #include <klocale.h>
19 #include <iostream>
20 KDiff3Part::KDiff3Part( QWidget *parentWidget, const char *widgetName,
21 QObject *parent, const char *name )
22 : KParts::ReadOnlyPart(parent, name)
23 {
24 // we need an instance
25 setInstance( KDiff3PartFactory::instance() );
26
27 // this should be your custom internal widget
28 m_widget = new KDiff3App( parentWidget, widgetName, this );
29
30 // This hack is necessary to avoid a crash when the program terminates.
31 m_bIsShell = dynamic_cast<KParts::MainWindow*>(parentWidget)!=0;
32
33 // notify the part that this is our internal widget
34 setWidget(m_widget);
35
36 // create our actions
37 //KStdAction::open(this, SLOT(fileOpen()), actionCollection());
38 //KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
39 //KStdAction::save(this, SLOT(save()), actionCollection());
40
41 setXMLFile("kdiff3_part.rc");
42
43 // we are read-write by default
44 setReadWrite(true);
45
46 // we are not modified since we haven't done anything yet
47 setModified(false);
48 }
49
50 KDiff3Part::~KDiff3Part()
51 {
52 if ( m_widget!=0 && ! m_bIsShell )
53 {
54 m_widget->saveOptions( m_widget->isPart() ? instance()->config() : kapp->config() );
55 }
56 }
57
58 void KDiff3Part::setReadWrite(bool /*rw*/)
59 {
60 // ReadWritePart::setReadWrite(rw);
61 }
62
63 void KDiff3Part::setModified(bool /*modified*/)
64 {
65 /*
66 // get a handle on our Save action and make sure it is valid
67 KAction *save = actionCollection()->action(KStdAction::stdName(KStdAction::Save));
68 if (!save)
69 return;
70
71 // if so, we either enable or disable it based on the current
72 // state
73 if (modified)
74 save->setEnabled(true);
75 else
76 save->setEnabled(false);
77
78 // in any event, we want our parent to do it's thing
79 ReadWritePart::setModified(modified);
80 */
81 }
82
83 bool KDiff3Part::openFile()
84 {
85 // m_file is always local so we can use QFile on it
86 QFile file(m_file);
87 if (file.open(IO_ReadOnly) == false)
88 return false;
89
90 // our example widget is text-based, so we use QTextStream instead
91 // of a raw QDataStream
92 QTextStream stream(&file);
93 QString str;
94 QString fileName1;
95 QString fileName2;
96 QString version1;
97 QString version2;
98 while (!stream.eof() && (fileName1.isEmpty() || fileName2.isEmpty()) )
99 {
100 str = stream.readLine() + "\n";
101 if ( str.left(4)=="--- " && fileName1.isEmpty() )
102 {
103 int pos = str.find("\t");
104 fileName1 = str.mid( 4, pos-4 );
105 int vpos = str.findRev("\t", -1);
106 if ( pos>0 && vpos>0 && vpos>pos )
107 {
108 version1 = str.mid( vpos+1 );
109 while( !version1.right(1)[0].isLetterOrNumber() )
110 version1.truncate( version1.length()-1 );
111 }
112 }
113 if ( str.left(4)=="+++ " && fileName2.isEmpty() )
114 {
115 int pos = str.find("\t");
116 fileName2 = str.mid( 4, pos-4 );
117 int vpos = str.findRev("\t", -1);
118 if ( pos>0 && vpos>0 && vpos>pos )
119 {
120 version2 = str.mid( vpos+1 );
121 while( !version2.right(1)[0].isLetterOrNumber() )
122 version2.truncate( version2.length()-1 );
123 }
124 }
125 }
126
127 file.close();
128
129 if ( fileName1.isEmpty() && fileName2.isEmpty() )
130 {
131 KMessageBox::sorry(m_widget, i18n("Couldn't find files for comparison."));
132 return false;
133 }
134
135 FileAccess f1(fileName1);
136 FileAccess f2(fileName2);
137
138 if ( f1.exists() && f2.exists() && fileName1!=fileName2 )
139 {
140 m_widget->slotFileOpen2( fileName1, fileName2, "", "", "", "", "" );
141 return true;
142 }
143 else if ( version1.isEmpty() && f1.exists() )
144 {
145 // Normal patch
146 // patch -f -u --ignore-whitespace -i [inputfile] -o [outfile] [patchfile]
147 QString tempFileName = FileAccess::tempFileName();
148 QString cmd = "patch -f -u --ignore-whitespace -i " + m_file +
149 " -o "+tempFileName + " " + fileName1;
150
151 ::system( cmd.ascii() );
152
153 m_widget->slotFileOpen2( fileName1, tempFileName, "", "",
154 "", version2.isEmpty() ? fileName2 : "REV:"+version2+":"+fileName2, "" ); // alias names
155 FileAccess::removeFile( tempFileName );
156 }
157 else if ( version2.isEmpty() && f2.exists() )
158 {
159 // Reverse patch
160 // patch -f -u -R --ignore-whitespace -i [inputfile] -o [outfile] [patchfile]
161 QString tempFileName = FileAccess::tempFileName();
162 QString cmd = "patch -f -u -R --ignore-whitespace -i " + m_file +
163 " -o "+tempFileName + " " + fileName2;
164
165 ::system( cmd.ascii() );
166
167 m_widget->slotFileOpen2( tempFileName, fileName2, "", "",
168 version1.isEmpty() ? fileName1 : "REV:"+version1+":"+fileName1, "", "" ); // alias name
169 FileAccess::removeFile( tempFileName );
170 }
171 else if ( !version1.isEmpty() && !version2.isEmpty() )
172 {
173 // Assuming that files are on CVS: Try to get them
174 // cvs update -p -r [REV] [FILE] > [OUTPUTFILE]
175
176 QString tempFileName1 = FileAccess::tempFileName();
177 QString cmd1 = "cvs update -p -r " + version1 + " " + fileName1 + " >"+tempFileName1;
178 ::system( cmd1.ascii() );
179
180 QString tempFileName2 = FileAccess::tempFileName();
181 QString cmd2 = "cvs update -p -r " + version2 + " " + fileName2 + " >"+tempFileName2;
182 ::system( cmd2.ascii() );
183
184 m_widget->slotFileOpen2( tempFileName1, tempFileName2, "", "",
185 "REV:"+version1+":"+fileName1,
186 "REV:"+version2+":"+fileName2,
187 ""
188 );
189
190 FileAccess::removeFile( tempFileName1 );
191 FileAccess::removeFile( tempFileName2 );
192 return true;
193 }
194 else
195 {
196 KMessageBox::sorry(m_widget, i18n("Couldn't find files for comparison."));
197 }
198
199 return true;
200 }
201
202 bool KDiff3Part::saveFile()
203 {
204 /* // if we aren't read-write, return immediately
205 if (isReadWrite() == false)
206 return false;
207
208 // m_file is always local, so we use QFile
209 QFile file(m_file);
210 if (file.open(IO_WriteOnly) == false)
211 return false;
212
213 // use QTextStream to dump the text to the file
214 QTextStream stream(&file);
215 //stream << m_widget->text();
216
217 file.close();
218 return true;
219 */
220 return false; // Not implemented
221 }
222
223
224 // It's usually safe to leave the factory code alone.. with the
225 // notable exception of the KAboutData data
226 #include <kaboutdata.h>
227 #include <klocale.h>
228
229 KInstance* KDiff3PartFactory::s_instance = 0L;
230 KAboutData* KDiff3PartFactory::s_about = 0L;
231
232 KDiff3PartFactory::KDiff3PartFactory()
233 : KParts::Factory()
234 {
235 }
236
237 KDiff3PartFactory::~KDiff3PartFactory()
238 {
239 delete s_instance;
240 delete s_about;
241
242 s_instance = 0L;
243 }
244
245 KParts::Part* KDiff3PartFactory::createPartObject( QWidget *parentWidget, const char *widgetName,
246 QObject *parent, const char *name,
247 const char *classname, const QStringList&/*args*/ )
248 {
249 // Create an instance of our Part
250 KDiff3Part* obj = new KDiff3Part( parentWidget, widgetName, parent, name );
251
252 // See if we are to be read-write or not
253 if (QCString(classname) == "KParts::ReadOnlyPart")
254 obj->setReadWrite(false);
255
256 return obj;
257 }
258
259 KInstance* KDiff3PartFactory::instance()
260 {
261 if( !s_instance )
262 {
263 s_about = new KAboutData("kdiff3part", I18N_NOOP("KDiff3Part"), "0.9.70");
264 s_about->addAuthor("Joachim Eibl", 0, "joachim.eibl@gmx.de");
265 s_instance = new KInstance(s_about);
266 }
267 return s_instance;
268 }
269
270 extern "C"
271 {
272 void* init_libkdiff3part()
273 {
274 return new KDiff3PartFactory;
275 }
276 };
277
278 #include "kdiff3_part.moc"