kdiff3plugin.cpp 9.37 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* This file is part of the KDiff3 project

   Copyright (C) 2008 Joachim Eibl <joachim dot eibl at gmx dot de>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; version 2
   of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include "kdiff3plugin.h"

23
#include <QApplication>
Joachim Eibl's avatar
Joachim Eibl committed
24
#include <kstandarddirs.h>
Michael Reeves's avatar
Michael Reeves committed
25
#include <QAction>
Joachim Eibl's avatar
Joachim Eibl committed
26
#include <kactionmenu.h>
27
#include <KLocalizedString>
Joachim Eibl's avatar
Joachim Eibl committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <kgenericfactory.h>
#include <kurl.h>
#include <kconfig.h>
#include <kconfiggroup.h>
#include <konq_popupmenuinformation.h>
#include <kmessagebox.h>
#include <kprocess.h>

//#include <iostream>

static QStringList* s_pHistory=0;

class KDiff3PluginFactory : public KGenericFactory < KDiff3Plugin, KonqPopupMenu >
{
   KConfig* m_pConfig;
   KConfigGroup* m_pConfigGroup;
public:
   KDiff3PluginFactory( const char* instanceName  = 0 )
   : KGenericFactory< KDiff3Plugin, KonqPopupMenu >( instanceName )
   {
      m_pConfig = 0;
      if (s_pHistory==0)
      {
         //std::cout << "New History: " << instanceName << std::endl;
         s_pHistory = new QStringList;
         m_pConfig = new KConfig( "kdiff3pluginrc", KConfig::SimpleConfig );
         m_pConfigGroup = new KConfigGroup( m_pConfig, "KDiff3Plugin" );
         *s_pHistory = m_pConfigGroup->readEntry("HistoryStack", QStringList() );
      }
   }

   ~KDiff3PluginFactory()
   {
      //std::cout << "Delete History" << std::endl;
      if ( s_pHistory && m_pConfigGroup )
         m_pConfigGroup->writeEntry("HistoryStack",*s_pHistory);
      delete s_pHistory;
      delete m_pConfigGroup;
      delete m_pConfig;
      s_pHistory = 0;
      m_pConfig = 0;
   }
};

K_EXPORT_COMPONENT_FACTORY (libkdiff3plugin, KDiff3PluginFactory ("kdiff3plugin"))

KDiff3Plugin::KDiff3Plugin( KonqPopupMenu* pPopupMenu, const QStringList & /* list */ )
:KonqPopupMenuPlugin(pPopupMenu)
{
77
   KLocalizedString::setApplicationDomain("kdiff3plugin");
Joachim Eibl's avatar
Joachim Eibl committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
   m_pPopupMenu = pPopupMenu;
   m_pParentWidget = pPopupMenu->parentWidget();
}

void KDiff3Plugin::setup( KActionCollection* actionCollection, const KonqPopupMenuInformation& popupMenuInfo, QMenu* pMenu )
{
   if (KStandardDirs::findExe("kdiff3").isNull ())
      return;

   // remember currently selected files (copy to a QStringList)
   KFileItemList itemList = popupMenuInfo.items();
   foreach ( const KFileItem& item, itemList )
   {
      //m_urlList.append( item.url() );
      m_list.append( item.url().url() );
   }


   /* Menu structure:
      KDiff3 -> (1 File selected):  Save 'selection' for later comparison (push onto history stack)
                                    Compare 'selection' with first file on history stack.
                                    Compare 'selection' with -> choice from history stack
                                    Merge 'selection' with first file on history stack.
                                    Merge 'selection' with last two files on history stack.
                (2 Files selected): Compare 's1' with 's2'
                                    Merge 's1' with 's2'
                (3 Files selected): Compare 's1', 's2' and 's3'
   */

   KActionMenu* pActionMenu = new KActionMenu (i18n ("KDiff3"), actionCollection );
Michael Reeves's avatar
Michael Reeves committed
108
   QAction * pAction = 0;
Joachim Eibl's avatar
Joachim Eibl committed
109
110
111
112
113
114
   QString s;

   if(m_list.count() == 1)
   {
      int historyCount = s_pHistory ? s_pHistory->count() : 0;
      s = i18n("Compare with %1", (historyCount>0 ? s_pHistory->front() : QString()) );
Michael Reeves's avatar
Michael Reeves committed
115
116
      pAction = new QAction ( s, actionCollection );
      connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotCompareWith);
Joachim Eibl's avatar
Joachim Eibl committed
117
118
119
120
      pAction->setEnabled( m_list.count()>0 && historyCount>0 );
      pActionMenu->addAction(pAction);

      s = i18n("Merge with %1", historyCount>0 ? s_pHistory->front() : QString() );
Michael Reeves's avatar
Michael Reeves committed
121
122
      pAction = new QAction( s, actionCollection);
      connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotMergeWith);
Joachim Eibl's avatar
Joachim Eibl committed
123
124
125
126
      pAction->setEnabled( m_list.count()>0 && historyCount>0 );
      pActionMenu->addAction (pAction);

      s = i18n("Save '%1' for later", ( m_list.front() ) );
Michael Reeves's avatar
Michael Reeves committed
127
128
      pAction = new QAction ( s, actionCollection);
      connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotSaveForLater);
Joachim Eibl's avatar
Joachim Eibl committed
129
130
131
      pAction->setEnabled( m_list.count()>0 );
      pActionMenu->addAction(pAction);

Michael Reeves's avatar
Michael Reeves committed
132
133
      pAction = new QAction (i18n("3-way merge with base"), actionCollection);
      connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotMergeThreeWay);
Joachim Eibl's avatar
Joachim Eibl committed
134
135
136
137
138
139
140
141
142
143
      pAction->setEnabled( m_list.count()>0 && historyCount>=2 );
      pActionMenu->addAction (pAction);

      if ( s_pHistory && !s_pHistory->empty() )
      {
         KActionMenu* pHistoryMenu = new KActionMenu( i18n("Compare with ..."), actionCollection );
         pHistoryMenu->setEnabled( m_list.count()>0 && historyCount>0 );
         pActionMenu->addAction(pHistoryMenu);
         for (QStringList::iterator i = s_pHistory->begin(); i!=s_pHistory->end(); ++i)
         {
Michael Reeves's avatar
Michael Reeves committed
144
145
            pAction = new QAction( *i, actionCollection);
            connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotCompareWithHistoryItem);
Joachim Eibl's avatar
Joachim Eibl committed
146
147
148
            pHistoryMenu->addAction (pAction);
         }

Michael Reeves's avatar
Michael Reeves committed
149
150
         pAction = new QAction (i18n("Clear list"), actionCollection);
         connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotClearList);
Joachim Eibl's avatar
Joachim Eibl committed
151
152
153
154
155
156
         pActionMenu->addAction (pAction);
         pAction->setEnabled( historyCount>0 );
      }
   }
   else if(m_list.count() == 2)
   {
Michael Reeves's avatar
Michael Reeves committed
157
158
      pAction = new QAction (i18n("Compare"), actionCollection);
      connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotCompareTwoFiles);
Joachim Eibl's avatar
Joachim Eibl committed
159
160
161
162
      pActionMenu->addAction (pAction);
   }
   else if ( m_list.count() == 3 )
   {
Michael Reeves's avatar
Michael Reeves committed
163
164
      pAction = new QAction (i18n("3 way comparison"), actionCollection);
      connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotCompareThreeFiles);
Joachim Eibl's avatar
Joachim Eibl committed
165
166
      pActionMenu->addAction (pAction);
   }
Michael Reeves's avatar
Michael Reeves committed
167
168
   pAction = new QAction (i18n("About KDiff3 menu plugin ..."), actionCollection);
   connect(pAction, &QAction::triggered, this, &KDiff3Plugin::slotAbout);
Joachim Eibl's avatar
Joachim Eibl committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
   pActionMenu->addAction (pAction);

   pMenu->addSeparator();
   pMenu->addAction( pActionMenu );
   pMenu->addSeparator();
}

KDiff3Plugin::~KDiff3Plugin ()
{
}

void KDiff3Plugin::slotCompareWith()
{
   if ( m_list.count() > 0 && s_pHistory && ! s_pHistory->empty() )
   {
      QStringList args;
      args << s_pHistory->front();
      args << m_list.front();
187
      KProcess::startDetached("kdiff3", args);
Joachim Eibl's avatar
Joachim Eibl committed
188
189
190
191
192
   }
}

void KDiff3Plugin::slotCompareWithHistoryItem()
{
Michael Reeves's avatar
Michael Reeves committed
193
   const QAction * pAction = dynamic_cast<const QAction*>( sender() );
Joachim Eibl's avatar
Joachim Eibl committed
194
195
196
197
198
   if ( m_list.count() > 0 && pAction )
   {
      QStringList args;
      args << pAction->text();
      args << m_list.front();
199
      KProcess::startDetached ("kdiff3", args);
Joachim Eibl's avatar
Joachim Eibl committed
200
201
202
203
204
205
206
207
208
209
   }
}

void KDiff3Plugin::slotCompareTwoFiles()
{
   if ( m_list.count() == 2 )
   {
      QStringList args;
      args << m_list.front();
      args << m_list.back();
210
      KProcess::startDetached ("kdiff3", args);
Joachim Eibl's avatar
Joachim Eibl committed
211
212
213
214
215
216
217
218
219
220
221
   }
}

void KDiff3Plugin::slotCompareThreeFiles()
{
   if ( m_list.count() == 3 )
   {
      QStringList args;
      args << m_list[0];
      args << m_list[1];
      args << m_list[2];
222
      KProcess::startDetached ("kdiff3", args);
Joachim Eibl's avatar
Joachim Eibl committed
223
224
225
226
227
228
229
230
231
232
233
   }
}

void KDiff3Plugin::slotMergeWith()
{
   if ( m_list.count() > 0 && s_pHistory && ! s_pHistory->empty() )
   {
      QStringList args;
      args << s_pHistory->front();
      args << m_list.front();
      args << ( "-o" + m_list.front() );
234
      KProcess::startDetached ("kdiff3", args);
Joachim Eibl's avatar
Joachim Eibl committed
235
236
237
238
239
240
241
242
243
244
245
246
   }
}

void KDiff3Plugin::slotMergeThreeWay()
{
   if ( m_list.count() > 0 && s_pHistory &&  s_pHistory->count()>=2 )
   {
      QStringList args;
      args << (*s_pHistory)[1];
      args << (*s_pHistory)[0];
      args << m_list.front();
      args << ("-o" + m_list.front());
247
      KProcess::startDetached ("kdiff3", args);
Joachim Eibl's avatar
Joachim Eibl committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
   }
}

void KDiff3Plugin::slotSaveForLater()
{
   if ( !m_list.isEmpty() && s_pHistory )
   {
      while ( s_pHistory->count()>=10 )
         s_pHistory->pop_back();
      s_pHistory->push_front( m_list.front() );
   }
}

void KDiff3Plugin::slotClearList()
{
   if ( s_pHistory )
      s_pHistory->clear();
}

void KDiff3Plugin::slotAbout()
{
   QString s = i18n("KDiff3 Menu Plugin: Copyright (C) 2008 Joachim Eibl\n"
                    "KDiff3 homepage: http://kdiff3.sourceforge.net\n\n");
Joachim Eibl's avatar
Joachim Eibl committed
271
   s += i18n("Using the context menu extension:\n"
Joachim Eibl's avatar
Joachim Eibl committed
272
             "For simple comparison of two selected files choose \"Compare\".\n"
Joachim Eibl's avatar
Joachim Eibl committed
273
274
275
276
277
278
279
             "If the other file is somewhere else, \"Save\" the first file for later, "
             "and it will appear in the \"Compare With ...\" submenu. "
             "Then, use \"Compare With\" on the second file.\n"
             "For a 3-way merge first, \"Save\" the base file, then the branch to merge, and "
             "then \"3-way merge with base\" on the other branch which will be used as the destination.\n"
             "The same also applies to directory comparison and merge.");
   KMessageBox::information(m_pParentWidget, s, i18n("About KDiff3 Menu Plugin") );
Joachim Eibl's avatar
Joachim Eibl committed
280
281
}