comparison kdiff3/kdiff3plugin-QT4/kdiff3plugin.cpp @ 81:5d7e91c44e6d

New files.
author joachim99
date Tue, 06 Jan 2009 18:01:57 +0000
parents
children
comparison
equal deleted inserted replaced
80:fcd146072e0c 81:5d7e91c44e6d
1 /* This file is part of the KDiff3 project
2
3 Copyright (C) 2008 Joachim Eibl <joachim dot eibl at gmx dot de>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; version 2
8 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21 #include "kdiff3plugin.h"
22
23 #include <kapplication.h>
24 #include <kstandarddirs.h>
25 #include <kaction.h>
26 #include <kactionmenu.h>
27 #include <klocale.h>
28 #include <kgenericfactory.h>
29 #include <kurl.h>
30 #include <kconfig.h>
31 #include <kconfiggroup.h>
32 #include <konq_popupmenuinformation.h>
33 #include <kmessagebox.h>
34 #include <kprocess.h>
35
36 //#include <iostream>
37
38 static QStringList* s_pHistory=0;
39
40 class KDiff3PluginFactory : public KGenericFactory < KDiff3Plugin, KonqPopupMenu >
41 {
42 KConfig* m_pConfig;
43 KConfigGroup* m_pConfigGroup;
44 public:
45 KDiff3PluginFactory( const char* instanceName = 0 )
46 : KGenericFactory< KDiff3Plugin, KonqPopupMenu >( instanceName )
47 {
48 m_pConfig = 0;
49 if (s_pHistory==0)
50 {
51 //std::cout << "New History: " << instanceName << std::endl;
52 s_pHistory = new QStringList;
53 m_pConfig = new KConfig( "kdiff3pluginrc", KConfig::SimpleConfig );
54 m_pConfigGroup = new KConfigGroup( m_pConfig, "KDiff3Plugin" );
55 *s_pHistory = m_pConfigGroup->readEntry("HistoryStack", QStringList() );
56 }
57 }
58
59 ~KDiff3PluginFactory()
60 {
61 //std::cout << "Delete History" << std::endl;
62 if ( s_pHistory && m_pConfigGroup )
63 m_pConfigGroup->writeEntry("HistoryStack",*s_pHistory);
64 delete s_pHistory;
65 delete m_pConfigGroup;
66 delete m_pConfig;
67 s_pHistory = 0;
68 m_pConfig = 0;
69 }
70 };
71
72 K_EXPORT_COMPONENT_FACTORY (libkdiff3plugin, KDiff3PluginFactory ("kdiff3plugin"))
73
74 KDiff3Plugin::KDiff3Plugin( KonqPopupMenu* pPopupMenu, const QStringList & /* list */ )
75 :KonqPopupMenuPlugin(pPopupMenu)
76 {
77 KGlobal::locale()->insertCatalog("kdiff3_plugin");
78 m_pPopupMenu = pPopupMenu;
79 m_pParentWidget = pPopupMenu->parentWidget();
80 }
81
82 void KDiff3Plugin::setup( KActionCollection* actionCollection, const KonqPopupMenuInformation& popupMenuInfo, QMenu* pMenu )
83 {
84 if (KStandardDirs::findExe("kdiff3").isNull ())
85 return;
86
87 // remember currently selected files (copy to a QStringList)
88 KFileItemList itemList = popupMenuInfo.items();
89 foreach ( const KFileItem& item, itemList )
90 {
91 //m_urlList.append( item.url() );
92 m_list.append( item.url().url() );
93 }
94
95
96 /* Menu structure:
97 KDiff3 -> (1 File selected): Save 'selection' for later comparison (push onto history stack)
98 Compare 'selection' with first file on history stack.
99 Compare 'selection' with -> choice from history stack
100 Merge 'selection' with first file on history stack.
101 Merge 'selection' with last two files on history stack.
102 (2 Files selected): Compare 's1' with 's2'
103 Merge 's1' with 's2'
104 (3 Files selected): Compare 's1', 's2' and 's3'
105 */
106
107 KActionMenu* pActionMenu = new KActionMenu (i18n ("KDiff3"), actionCollection );
108 KAction* pAction = 0;
109 QString s;
110
111 if(m_list.count() == 1)
112 {
113 int historyCount = s_pHistory ? s_pHistory->count() : 0;
114 s = i18n("Compare with %1", (historyCount>0 ? s_pHistory->front() : QString()) );
115 pAction = new KAction ( s, actionCollection );
116 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotCompareWith()));
117 pAction->setEnabled( m_list.count()>0 && historyCount>0 );
118 pActionMenu->addAction(pAction);
119
120 s = i18n("Merge with %1", historyCount>0 ? s_pHistory->front() : QString() );
121 pAction = new KAction( s, actionCollection);
122 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotMergeWith()));
123 pAction->setEnabled( m_list.count()>0 && historyCount>0 );
124 pActionMenu->addAction (pAction);
125
126 s = i18n("Save '%1' for later", ( m_list.front() ) );
127 pAction = new KAction ( s, actionCollection);
128 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotSaveForLater()));
129 pAction->setEnabled( m_list.count()>0 );
130 pActionMenu->addAction(pAction);
131
132 pAction = new KAction (i18n("3-way merge with base"), actionCollection);
133 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotMergeThreeWay()));
134 pAction->setEnabled( m_list.count()>0 && historyCount>=2 );
135 pActionMenu->addAction (pAction);
136
137 if ( s_pHistory && !s_pHistory->empty() )
138 {
139 KActionMenu* pHistoryMenu = new KActionMenu( i18n("Compare with ..."), actionCollection );
140 pHistoryMenu->setEnabled( m_list.count()>0 && historyCount>0 );
141 pActionMenu->addAction(pHistoryMenu);
142 for (QStringList::iterator i = s_pHistory->begin(); i!=s_pHistory->end(); ++i)
143 {
144 pAction = new KAction( *i, actionCollection);
145 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotCompareWithHistoryItem()));
146 pHistoryMenu->addAction (pAction);
147 }
148
149 pAction = new KAction (i18n("Clear list"), actionCollection);
150 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotClearList()));
151 pActionMenu->addAction (pAction);
152 pAction->setEnabled( historyCount>0 );
153 }
154 }
155 else if(m_list.count() == 2)
156 {
157 pAction = new KAction (i18n("Compare"), actionCollection);
158 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotCompareTwoFiles()));
159 pActionMenu->addAction (pAction);
160 }
161 else if ( m_list.count() == 3 )
162 {
163 pAction = new KAction (i18n("3 way comparison"), actionCollection);
164 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotCompareThreeFiles()));
165 pActionMenu->addAction (pAction);
166 }
167 pAction = new KAction (i18n("About KDiff3 menu plugin ..."), actionCollection);
168 connect( pAction, SIGNAL(triggered(bool)), this, SLOT(slotAbout()));
169 pActionMenu->addAction (pAction);
170
171 pMenu->addSeparator();
172 pMenu->addAction( pActionMenu );
173 pMenu->addSeparator();
174 }
175
176 KDiff3Plugin::~KDiff3Plugin ()
177 {
178 }
179
180 void KDiff3Plugin::slotCompareWith()
181 {
182 if ( m_list.count() > 0 && s_pHistory && ! s_pHistory->empty() )
183 {
184 QStringList args;
185 args << s_pHistory->front();
186 args << m_list.front();
187 KProcess::execute("kdiff3", args);
188 }
189 }
190
191 void KDiff3Plugin::slotCompareWithHistoryItem()
192 {
193 const KAction* pAction = dynamic_cast<const KAction*>( sender() );
194 if ( m_list.count() > 0 && pAction )
195 {
196 QStringList args;
197 args << pAction->text();
198 args << m_list.front();
199 KProcess::execute ("kdiff3", args);
200 }
201 }
202
203 void KDiff3Plugin::slotCompareTwoFiles()
204 {
205 if ( m_list.count() == 2 )
206 {
207 QStringList args;
208 args << m_list.front();
209 args << m_list.back();
210 KProcess::execute ("kdiff3", args);
211 }
212 }
213
214 void KDiff3Plugin::slotCompareThreeFiles()
215 {
216 if ( m_list.count() == 3 )
217 {
218 QStringList args;
219 args << m_list[0];
220 args << m_list[1];
221 args << m_list[2];
222 KProcess::execute ("kdiff3", args);
223 }
224 }
225
226 void KDiff3Plugin::slotMergeWith()
227 {
228 if ( m_list.count() > 0 && s_pHistory && ! s_pHistory->empty() )
229 {
230 QStringList args;
231 args << s_pHistory->front();
232 args << m_list.front();
233 args << ( "-o" + m_list.front() );
234 KProcess::execute ("kdiff3", args);
235 }
236 }
237
238 void KDiff3Plugin::slotMergeThreeWay()
239 {
240 if ( m_list.count() > 0 && s_pHistory && s_pHistory->count()>=2 )
241 {
242 QStringList args;
243 args << (*s_pHistory)[1];
244 args << (*s_pHistory)[0];
245 args << m_list.front();
246 args << ("-o" + m_list.front());
247 KProcess::execute ("kdiff3", args);
248 }
249 }
250
251 void KDiff3Plugin::slotSaveForLater()
252 {
253 if ( !m_list.isEmpty() && s_pHistory )
254 {
255 while ( s_pHistory->count()>=10 )
256 s_pHistory->pop_back();
257 s_pHistory->push_front( m_list.front() );
258 }
259 }
260
261 void KDiff3Plugin::slotClearList()
262 {
263 if ( s_pHistory )
264 s_pHistory->clear();
265 }
266
267 void KDiff3Plugin::slotAbout()
268 {
269 QString s = i18n("KDiff3 Menu Plugin: Copyright (C) 2008 Joachim Eibl\n"
270 "KDiff3 homepage: http://kdiff3.sourceforge.net\n\n");
271 s += i18n("Using the contextmenu extension:\n"
272 "For simple comparison of two selected files choose \"Compare\".\n"
273 "If the other file is somewhere else \"Save\" the first file for later. "
274 "It will appear in the \"Compare With ...\" submenu. "
275 "Then use \"Compare With\" on second file.\n"
276 "For a 3-way merge first \"Save\" the base file, then the branch to merge and "
277 "choose \"3-way merge with base\" on the other branch which will be used as destination.\n"
278 "Same also applies to directory comparison and merge.");
279 KMessageBox::information(m_pParentWidget, s, tr("About KDiff3 Menu Plugin") );
280 }
281