• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

kompare

  • sources
  • kde-4.12
  • kdesdk
  • kompare
kompare_shell.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  kompare_shell.cpp
3  -----------------
4  begin : Sun Mar 4 2001
5  Copyright 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
6  Copyright 2001-2003 John Firebaugh <jfirebaugh@kde.org>
7  Copyright 2007-2011 Kevin Kofler <kevin.kofler@chello.at>
8 ****************************************************************************/
9 
10 /***************************************************************************
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ***************************************************************************/
18 #include "kompare_shell.h"
19 
20 #include <QtCore/QTextStream>
21 #include <QtGui/QDockWidget>
22 
23 #include <ktexteditor/document.h>
24 #include <ktexteditor/view.h>
25 #include <kdebug.h>
26 #include <kedittoolbar.h>
27 #include <kencodingfiledialog.h>
28 #include <kiconloader.h>
29 #include <kshortcutsdialog.h>
30 #include <klibloader.h>
31 #include <klocale.h>
32 #include <kmessagebox.h>
33 #include <kparts/componentfactory.h>
34 #include <ksqueezedtextlabel.h>
35 #include <kstatusbar.h>
36 #include <kstandardaction.h>
37 #include <kmimetypetrader.h>
38 #include <kservicetypetrader.h>
39 #include <ktoggleaction.h>
40 // #include <kuserprofile.h>
41 #include <kactioncollection.h>
42 
43 #include "kompareinterface.h"
44 #include "kompareurldialog.h"
45 
46 #define ID_N_OF_N_DIFFERENCES 1
47 #define ID_N_OF_N_FILES 2
48 #define ID_GENERAL 3
49 
50 KompareShell::KompareShell()
51  : KParts::MainWindow( ),
52  m_textViewPart( 0 ),
53  m_textViewWidget( 0 )
54 {
55  if ( !initialGeometrySet() )
56  resize( 800, 480 );
57 
58  // set the shell's ui resource file
59  setXMLFile("kompareui.rc");
60 
61  // then, setup our actions
62  setupActions();
63  setupStatusBar();
64 
65  m_viewPart = KMimeTypeTrader::createInstanceFromQuery<KParts::ReadWritePart>("text/x-patch", "Kompare/ViewPart", this);
66 
67  if ( m_viewPart )
68  {
69  setCentralWidget( m_viewPart->widget() );
70  // and integrate the part's GUI with the shell's
71  createGUI(m_viewPart);
72  }
73  else
74  {
75  // if we couldn't load our Part, we exit since the Shell by
76  // itself can't do anything useful
77  KMessageBox::error(this, i18n( "Could not load our KompareViewPart." ) );
78  exit(2);
79  }
80 
81  m_navTreeDock = new QDockWidget( i18n( "Navigation" ), this );
82  m_navTreeDock->setObjectName( "Navigation" );
83 
84  // This part is implemented in KompareNavTreePart
85  m_navTreePart = KServiceTypeTrader::createInstanceFromQuery<KParts::ReadOnlyPart>
86  ("KParts/ReadOnlyPart", "'Kompare/NavigationPart' in ServiceTypes", m_navTreeDock);
87 
88  if ( m_navTreePart )
89  {
90  m_navTreeDock->setWidget( m_navTreePart->widget() );
91  addDockWidget( Qt::TopDockWidgetArea, m_navTreeDock );
92 // m_navTreeDock->manualDock( m_mainViewDock, KDockWidget::DockTop, 20 );
93  }
94  else
95  {
96  // if we couldn't load our Part, we exit since the Shell by
97  // itself can't do anything useful
98  KMessageBox::error(this, i18n( "Could not load our KompareNavigationPart." ) );
99  exit(4);
100  }
101 
102  // Hook up the inter part communication
103  connect( m_viewPart, SIGNAL( modelsChanged(const Diff2::DiffModelList*) ),
104  m_navTreePart, SLOT( slotModelsChanged( const Diff2::DiffModelList*) ) );
105 
106  connect( m_viewPart, SIGNAL( kompareInfo(Kompare::Info*) ),
107  m_navTreePart, SLOT( slotKompareInfo(Kompare::Info*) ) );
108 
109  connect( m_navTreePart, SIGNAL( selectionChanged(const Diff2::DiffModel*, const Diff2::Difference*) ),
110  m_viewPart, SIGNAL( selectionChanged(const Diff2::DiffModel*, const Diff2::Difference*) ) );
111  connect( m_viewPart, SIGNAL( setSelection(const Diff2::DiffModel*, const Diff2::Difference*) ),
112  m_navTreePart, SLOT( slotSetSelection(const Diff2::DiffModel*, const Diff2::Difference*) ) );
113 
114  connect( m_navTreePart, SIGNAL( selectionChanged(const Diff2::Difference*) ),
115  m_viewPart, SIGNAL( selectionChanged(const Diff2::Difference*) ) );
116  connect( m_viewPart, SIGNAL( setSelection(const Diff2::Difference*) ),
117  m_navTreePart, SLOT( slotSetSelection(const Diff2::Difference*) ) );
118 
119  // This is the interpart interface, it is signal and slot based so no "real" nterface here
120  // All you have to do is connect the parts from your application.
121  // These just point to the method with the same name in the KompareModelList or get called
122  // from the method with the same name in KompareModelList.
123 
124  // There is currently no applying possible from the navtreepart to the viewpart
125  connect( m_viewPart, SIGNAL(applyDifference(bool)),
126  m_navTreePart, SLOT(slotApplyDifference(bool)) );
127  connect( m_viewPart, SIGNAL(applyAllDifferences(bool)),
128  m_navTreePart, SLOT(slotApplyAllDifferences(bool)) );
129  connect( m_viewPart, SIGNAL(applyDifference(const Diff2::Difference*, bool)),
130  m_navTreePart, SLOT(slotApplyDifference(const Diff2::Difference*, bool)) );
131 
132  // Hook up the KomparePart -> KompareShell communication
133  connect( m_viewPart, SIGNAL( setStatusBarModelInfo( int, int, int, int, int ) ),
134  this, SLOT( slotUpdateStatusBar( int, int, int, int, int ) ) );
135  connect( m_viewPart, SIGNAL( setStatusBarText(const QString&) ),
136  this, SLOT( slotSetStatusBarText(const QString&) ) );
137 
138  connect( m_viewPart, SIGNAL(diffString(const QString&)),
139  this, SLOT(slotSetDiffString(const QString&)) );
140 
141  // Read basic main-view settings, and set to autosave
142  setAutoSaveSettings( "General Options" );
143 }
144 
145 KompareShell::~KompareShell()
146 {
147 }
148 
149 bool KompareShell::queryClose()
150 {
151  bool rv = m_viewPart->queryClose();
152  if ( rv )
153  KGlobal::deref();
154  return rv;
155 }
156 
157 void KompareShell::openDiff(const KUrl& url)
158 {
159  kDebug(8102) << "Url = " << url.prettyUrl() << endl;
160  m_diffURL = url;
161  viewPart()->openDiff( url );
162 }
163 
164 void KompareShell::openStdin()
165 {
166  kDebug(8102) << "Using stdin to read the diff" << endl;
167  QFile file;
168  file.open( stdin, QIODevice::ReadOnly );
169  QTextStream stream( &file );
170 
171  QString diff = stream.readAll();
172 
173  file.close();
174 
175  viewPart()->openDiff( diff );
176 
177 }
178 
179 void KompareShell::compare(const KUrl& source,const KUrl& destination )
180 {
181  m_sourceURL = source;
182  m_destinationURL = destination;
183 
184  viewPart()->compare( source, destination );
185 }
186 
187 void KompareShell::blend( const KUrl& url1, const KUrl& diff )
188 {
189  m_sourceURL = url1;
190  m_destinationURL = diff;
191 
192  viewPart()->openDirAndDiff( url1, diff );
193 }
194 
195 void KompareShell::setupActions()
196 {
197  KAction *a;
198  a = actionCollection()->addAction(KStandardAction::Open, this, SLOT(slotFileOpen()));
199  a->setText( i18n( "&Open Diff..." ) );
200  a = actionCollection()->addAction("file_compare_files", this, SLOT(slotFileCompareFiles()));
201  a->setIcon(KIcon("document-open"));
202  a->setText(i18n("&Compare Files..."));
203  a->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
204  a = actionCollection()->addAction("file_blend_url", this, SLOT(slotFileBlendURLAndDiff()));
205  a->setText(i18n("&Blend URL with Diff..."));
206  a->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
207  actionCollection()->addAction(KStandardAction::Quit, this, SLOT( slotFileClose() ));
208 
209  createStandardStatusBarAction();
210  setStandardToolBarMenuEnabled(true);
211  m_showTextView = new KToggleAction(i18n("Show T&ext View"), this);
212 // needs a KGuiItem, also the doc says explicitly not to do this
213 // m_showTextView->setCheckedState(i18n("Hide T&ext View"));
214  actionCollection()->addAction("options_show_text_view", m_showTextView);
215  connect(m_showTextView, SIGNAL(triggered(bool)), SLOT(slotShowTextView()));
216 
217  KStandardAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
218  KStandardAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
219 }
220 
221 void KompareShell::setupStatusBar()
222 {
223  // Made these entries permanent so they will appear on the right side
224  statusBar()->insertPermanentItem( i18n(" 0 of 0 differences "), ID_N_OF_N_DIFFERENCES, 0);
225  statusBar()->insertPermanentItem( i18n(" 0 of 0 files "), ID_N_OF_N_FILES, 0);
226 
227  m_generalLabel = new KSqueezedTextLabel( "", 0 );
228  statusBar()->addWidget( m_generalLabel, 1 );
229  m_generalLabel->setAlignment( Qt::AlignLeft );
230 }
231 
232 void KompareShell::slotUpdateStatusBar( int modelIndex, int differenceIndex, int modelCount, int differenceCount, int appliedCount )
233 {
234  kDebug(8102) << "KompareShell::updateStatusBar()" << endl;
235 
236  QString fileStr;
237  QString diffStr;
238 
239  if ( modelIndex >= 0 )
240  fileStr = i18np( " %2 of %1 file ", " %2 of %1 files ", modelCount, modelIndex + 1 );
241  else
242  fileStr = i18np( " %1 file ", " %1 files ", modelCount );
243 
244  if ( differenceIndex >= 0 )
245  diffStr = i18np( " %2 of %1 difference, %3 applied ", " %2 of %1 differences, %3 applied ", differenceCount ,
246  differenceIndex + 1, appliedCount );
247  else
248  diffStr = i18np( " %1 difference ", " %1 differences ", differenceCount );
249 
250  statusBar()->changeItem( fileStr, ID_N_OF_N_FILES );
251  statusBar()->changeItem( diffStr, ID_N_OF_N_DIFFERENCES );
252 }
253 
254 void KompareShell::slotSetStatusBarText( const QString& text )
255 {
256  m_generalLabel->setText( text );
257 }
258 
259 void KompareShell::saveProperties(KConfigGroup &config)
260 {
261  // The 'config' object points to the session managed
262  // config file. Anything you write here will be available
263  // later when this app is restored
264  if ( m_mode == Kompare::ComparingFiles )
265  {
266  config.writeEntry( "Mode", "ComparingFiles" );
267  config.writePathEntry( "SourceUrl", m_sourceURL.url() );
268  config.writePathEntry( "DestinationUrl", m_destinationURL.url() );
269  }
270  else if ( m_mode == Kompare::ShowingDiff )
271  {
272  config.writeEntry( "Mode", "ShowingDiff" );
273  config.writePathEntry( "DiffUrl", m_diffURL.url() );
274  }
275 
276  viewPart()->saveProperties( config.config() );
277 }
278 
279 void KompareShell::readProperties(const KConfigGroup &config)
280 {
281  // The 'config' object points to the session managed
282  // config file. This function is automatically called whenever
283  // the app is being restored. Read in here whatever you wrote
284  // in 'saveProperties'
285 
286  QString mode = config.readEntry( "Mode", "ComparingFiles" );
287  if ( mode == "ComparingFiles" )
288  {
289  m_mode = Kompare::ComparingFiles;
290  m_sourceURL = config.readPathEntry( "SourceUrl", "" );
291  m_destinationURL = config.readPathEntry( "DestinationFile", "" );
292 
293  viewPart()->readProperties( const_cast<KConfig *>(config.config()) );
294 
295  viewPart()->compareFiles( m_sourceURL, m_destinationURL );
296  }
297  else if ( mode == "ShowingDiff" )
298  {
299  m_mode = Kompare::ShowingDiff;
300  m_diffURL = config.readPathEntry( "DiffUrl", "" );
301 
302  viewPart()->readProperties( const_cast<KConfig *>(config.config()) );
303 
304  m_viewPart->openUrl( m_diffURL );
305  }
306  else
307  { // just in case something weird has happened, don't restore the diff then
308  // Bruggie: or when some idiot like me changes the possible values for mode
309  // IOW, a nice candidate for a kconf_update thingy :)
310  viewPart()->readProperties( const_cast<KConfig *>(config.config()) );
311  }
312 }
313 
314 void KompareShell::slotFileOpen()
315 {
316  // FIXME: use different filedialog which gets encoding
317  KUrl url = KFileDialog::getOpenUrl( KUrl(), "text/x-patch", this );
318  if( !url.isEmpty() ) {
319  KompareShell* shell = new KompareShell();
320  KGlobal::ref();
321  shell->show();
322  shell->openDiff( url );
323  }
324 }
325 
326 void KompareShell::slotFileBlendURLAndDiff()
327 {
328  KompareURLDialog dialog( this );
329 
330  dialog.setCaption( i18n( "Blend File/Folder with diff Output" ) );
331  dialog.setFirstGroupBoxTitle( i18n( "File/Folder" ) );
332  dialog.setSecondGroupBoxTitle( i18n( "Diff Output" ) );
333 
334  KGuiItem blendGuiItem( i18n( "Blend" ), QString(), i18n( "Blend this file or folder with the diff output" ), i18n( "If you have entered a file or folder name and a file that contains diff output in the fields in this dialog then this button will be enabled and pressing it will open kompare's main view where the output of the entered file or files from the folder are mixed with the diff output so you can then apply the difference(s) to a file or to the files. " ) );
335  dialog.setButtonGuiItem( KDialog::Ok, blendGuiItem );
336 
337  dialog.setGroup( "Recent Blend Files" );
338 
339  dialog.setFirstURLRequesterMode( KFile::File|KFile::Directory|KFile::ExistingOnly );
340  // diff output can not be a directory
341  dialog.setSecondURLRequesterMode( KFile::File|KFile::ExistingOnly );
342  if ( dialog.exec() == QDialog::Accepted )
343  {
344  m_sourceURL = dialog.getFirstURL();
345  m_destinationURL = dialog.getSecondURL();
346  // Leak???
347  KompareShell* shell = new KompareShell();
348  KGlobal::ref();
349  shell->show();
350  shell->viewPart()->setEncoding( dialog.encoding() );
351  shell->blend( m_sourceURL, m_destinationURL );
352  }
353 }
354 
355 void KompareShell::slotFileCompareFiles()
356 {
357  KompareURLDialog dialog( this );
358 
359  dialog.setCaption( i18n( "Compare Files or Folders" ) );
360  dialog.setFirstGroupBoxTitle( i18n( "Source" ) );
361  dialog.setSecondGroupBoxTitle( i18n( "Destination" ) );
362 
363  KGuiItem compareGuiItem( i18n( "Compare" ), QString(), i18n( "Compare these files or folders" ), i18n( "If you have entered 2 filenames or 2 folders in the fields in this dialog then this button will be enabled and pressing it will start a comparison of the entered files or folders. " ) );
364  dialog.setButtonGuiItem( KDialog::Ok, compareGuiItem );
365 
366  dialog.setGroup( "Recent Compare Files" );
367 
368  dialog.setFirstURLRequesterMode( KFile::File|KFile::Directory|KFile::ExistingOnly );
369  dialog.setSecondURLRequesterMode( KFile::File|KFile::Directory|KFile::ExistingOnly );
370 
371  if ( dialog.exec() == QDialog::Accepted )
372  {
373  m_sourceURL = dialog.getFirstURL();
374  m_destinationURL = dialog.getSecondURL();
375  KompareShell* shell = new KompareShell();
376  KGlobal::ref();
377  shell->show();
378  kDebug(8102) << "The encoding is: " << dialog.encoding() << endl;
379  shell->viewPart()->setEncoding( dialog.encoding() );
380  shell->compare( m_sourceURL, m_destinationURL );
381  }
382 }
383 
384 void KompareShell::slotFileClose()
385 {
386  if ( m_viewPart->queryClose() )
387  {
388  KGlobal::deref();
389  }
390 }
391 
392 void KompareShell::slotShowTextView()
393 {
394  if ( !m_textViewWidget )
395  {
396  QString error;
397 
398  // FIXME: proper error checking
399  m_textViewWidget = new QDockWidget( i18n( "Text View" ), this );
400  m_textViewWidget->setObjectName( "Text View" );
401 // m_textViewWidget = createDockWidget( i18n("Text View"), SmallIcon( "text-x-generic") );
402  m_textViewPart = KServiceTypeTrader::createInstanceFromQuery<KTextEditor::Document>(
403  QString::fromLatin1("KTextEditor/Document"),
404  this, this, QString(), QVariantList(), &error );
405  if ( m_textViewPart )
406  {
407  m_textView = qobject_cast<KTextEditor::View*>( m_textViewPart->createView( this ) );
408  m_textViewWidget->setWidget( static_cast<QWidget*>(m_textView) );
409  m_textViewPart->setHighlightingMode( "Diff" );
410  m_textViewPart->setText( m_diffString );
411  }
412  m_textViewWidget->show();
413  connect( m_textViewWidget, SIGNAL(visibilityChanged(bool)), SLOT(slotVisibilityChanged(bool)) );
414  }
415  else if ( m_textViewWidget->isVisible() )
416  m_textViewWidget->hide();
417  else
418  m_textViewWidget->show();
419 
420  addDockWidget( Qt::BottomDockWidgetArea, m_textViewWidget );
421 // m_textViewWidget->manualDock( m_mainViewDock, KDockWidget:: DockCenter );
422 }
423 
424 void KompareShell::slotVisibilityChanged( bool visible )
425 {
426  m_showTextView->setChecked( visible );
427 }
428 
429 void KompareShell::slotSetDiffString( const QString& diffString )
430 {
431  if ( m_textViewPart )
432  m_textViewPart->setText( diffString );
433  m_diffString = diffString;
434 }
435 
436 void KompareShell::optionsConfigureKeys()
437 {
438  KShortcutsDialog dlg( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this );
439 
440  dlg.addCollection( actionCollection() );
441  if ( m_viewPart )
442  dlg.addCollection( m_viewPart->actionCollection() );
443 
444  dlg.configure();
445 }
446 
447 void KompareShell::optionsConfigureToolbars()
448 {
449  KConfigGroup group(KGlobal::config(), autoSaveGroup());
450  saveMainWindowSettings(group);
451  // use the standard toolbar editor
452  KEditToolBar dlg(factory());
453  connect(&dlg,SIGNAL(newToolbarConfig()),this,SLOT(newToolbarConfig()));
454  dlg.exec();
455 }
456 
457 void KompareShell::newToolbarConfig()
458 {
459  KConfigGroup group(KGlobal::config(), autoSaveGroup());
460  applyMainWindowSettings(group);
461 }
462 
463 KompareInterface* KompareShell::viewPart() const
464 {
465  return qobject_cast<KompareInterface *>(m_viewPart);
466 }
467 
468 #include "kompare_shell.moc"
ID_N_OF_N_FILES
#define ID_N_OF_N_FILES
Definition: kompare_shell.cpp:47
KompareShell::queryClose
virtual bool queryClose()
Definition: kompare_shell.cpp:149
KompareURLDialog
Definition of class KompareURLDialog.
Definition: kompareurldialog.h:38
KompareInterface::setEncoding
virtual void setEncoding(const QString &encoding)
This will set the encoding to use for all files that are read or for the diffoutput.
Definition: kompareinterface.cpp:72
KompareInterface::compareFiles
virtual void compareFiles(const KUrl &sourceFile, const KUrl &destinationFile)=0
Compare, with diff, source with destination files.
KompareShell::blend
void blend(const KUrl &url1, const KUrl &diff)
Use this method to blend diff into url1 (file or directory)
Definition: kompare_shell.cpp:187
KompareShell::openStdin
void openStdin()
Use this method to load the diff from stdin.
Definition: kompare_shell.cpp:164
KompareInterface
Definition: kompareinterface.h:32
KompareInterface::saveProperties
virtual int saveProperties(KConfig *config)=0
kompare_shell.h
KompareShell::slotUpdateStatusBar
void slotUpdateStatusBar(int modelIndex, int differenceIndex, int modelCount, int differenceCount, int appliedCount)
Definition: kompare_shell.cpp:232
KompareShell::saveProperties
void saveProperties(KConfigGroup &)
This method is called when it is time for the app to save its properties for session management purpo...
Definition: kompare_shell.cpp:259
KompareInterface::readProperties
virtual int readProperties(KConfig *config)=0
Warning this should be in class Part in KDE 4.0, not here ! Around that time the methods will disappe...
kompareurldialog.h
KompareInterface::openDirAndDiff
virtual void openDirAndDiff(const KUrl &dir, const KUrl &diffFile)=0
This will show the directory and the directory with the diff applied.
kompareinterface.h
KompareShell::~KompareShell
virtual ~KompareShell()
Default Destructor.
Definition: kompare_shell.cpp:145
KompareShell::readProperties
void readProperties(const KConfigGroup &)
This method is called when this app is restored.
Definition: kompare_shell.cpp:279
ID_N_OF_N_DIFFERENCES
#define ID_N_OF_N_DIFFERENCES
Definition: kompare_shell.cpp:46
KompareShell::viewPart
KompareInterface * viewPart() const
Definition: kompare_shell.cpp:463
KompareShell::compare
void compare(const KUrl &source, const KUrl &destination)
Use this method to compare 2 URLs (files or directories)
Definition: kompare_shell.cpp:179
KompareInterface::openDiff
virtual bool openDiff(const KUrl &diffUrl)=0
Open and parse the diff file at url.
KompareShell::openDiff
void openDiff(const KUrl &url)
Use this method to load whatever file/URL you have.
Definition: kompare_shell.cpp:157
KompareInterface::compare
virtual void compare(const KUrl &sourceFile, const KUrl &destinationFile)=0
Compare, with diff, source with destination, can also be used if you do not know what source and dest...
KompareShell
This is the application "Shell".
Definition: kompare_shell.h:55
KompareShell::KompareShell
KompareShell()
Default Constructor.
Definition: kompare_shell.cpp:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kompare

Skip menu "kompare"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal