main.cpp 10.9 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
2
3
4
/***************************************************************************
                          main.cpp  -  Where everything starts.
                             -------------------
    begin                : Don Jul 11 12:31:29 CEST 2002
Joachim Eibl's avatar
Joachim Eibl committed
5
    copyright            : (C) 2002-2007 by Joachim Eibl
Joachim Eibl's avatar
Joachim Eibl committed
6
    email                : joachim.eibl at gmx.de
Joachim Eibl's avatar
Joachim Eibl committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <klocale.h>
#include "kdiff3_shell.h"
Joachim Eibl's avatar
Joachim Eibl committed
22
#include <kstandarddirs.h>
23
#include "version.h"
Joachim Eibl's avatar
Joachim Eibl committed
24
25
26
#include <QTextCodec>
#include <QFile>
#include <QTextStream>
Joachim Eibl's avatar
Joachim Eibl committed
27
#include <QTranslator>
Joachim Eibl's avatar
Joachim Eibl committed
28
#include <QLocale>
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
29
#include <QFont>
30
#include <QClipboard>
Joachim Eibl's avatar
Joachim Eibl committed
31
#include <vector>
Joachim Eibl's avatar
Joachim Eibl committed
32

Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
33
34
35
#ifdef KREPLACEMENTS_H
#include "optiondialog.h"
#endif
Joachim Eibl's avatar
Joachim Eibl committed
36
#include "common.h"
Joachim Eibl's avatar
Joachim Eibl committed
37

Joachim Eibl's avatar
Joachim Eibl committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void initialiseCmdLineArgs( KCmdLineOptions& options )
{
   QString configFileName = KStandardDirs().findResource("config","kdiff3rc");
   QFile configFile( configFileName );
   QString ignorableOptionsLine = "-u;-query;-html;-abort";
   if ( configFile.open( QIODevice::ReadOnly ) )
   {
      QTextStream ts( &configFile );
      while(!ts.atEnd())
      {
         QString line = ts.readLine();
         if ( line.startsWith("IgnorableCmdLineOptions=") )
         {
            int pos = line.indexOf('=');
            if (pos>=0)
            {
               ignorableOptionsLine = line.mid(pos+1);
            }
            break;
         }
      }
   }
Joachim Eibl's avatar
Joachim Eibl committed
60

Joachim Eibl's avatar
Joachim Eibl committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifdef KREPLACEMENTS_H
   QStringList sl = ignorableOptionsLine.split( '|' );
#else
   QStringList sl = ignorableOptionsLine.split( ',' );
#endif
   if (!sl.isEmpty())
   {
      QStringList ignorableOptions = sl.front().split( ';' );
      for (QStringList::iterator i=ignorableOptions.begin(); i!=ignorableOptions.end(); ++i)
      {
         (*i).remove('-');
         if (!(*i).isEmpty())
         {
            if ( i->length()==1 )
               options.add( i->toLatin1() ).add("ignore", ki18n("Ignored. (User defined.)") );
            else
               options.add( i->toLatin1(), ki18n("Ignored. (User defined.)") );
         }
      }
   }
}
Joachim Eibl's avatar
Joachim Eibl committed
82

Joachim Eibl's avatar
Joachim Eibl committed
83
84
85
86
87
88
89
#ifdef _WIN32
#include <process.h>
// This command checks the comm
static bool isOptionUsed(const QString& s, int argc, char* argv[])
{
   for(int j=0; j<argc; ++j )
   {
90
      if( QString("-"+s) == argv[j] || QString("--"+s)==argv[j] )
Joachim Eibl's avatar
Joachim Eibl committed
91
92
93
94
95
96
97
98
      {
         return true;
      }
   }
   return false;
}
#endif

Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
99
100
101
102
103
104
105
106
107
108
109
110
111
class ContextFreeTranslator : public QTranslator
{
public:
   ContextFreeTranslator( QObject* pParent ) : QTranslator(pParent) {}
   QString translate(const char* context, const char* sourceText, const char* comment ) const
   {
      if ( context != 0 )
         return QTranslator::translate(0,sourceText,comment);
      else
         return QString();
   }
};

Joachim Eibl's avatar
Joachim Eibl committed
112
113
114
115
116
117
int main(int argc, char *argv[])
{
#ifdef _WIN32
   /* KDiff3 can be used as replacement for the text-diff and merge tool provided by
      Clearcase. This is experimental and so far has only been tested under Windows.

Joachim Eibl's avatar
Joachim Eibl committed
118
119
120
121
122
123
124
125
126
127
128
129
      There are two ways to use KDiff3 with clearcase
      -  The file lib/mgrs/map contains the list of compare/merge tasks on one side and 
         the tool on the other. Originally this contains only clearcase tools, but you can
         edit this file and put kdiff3 there instead. (Recommended method)
      -  Exchange the original program with KDiff3: (Hackish, no fine control)
         1. In the Clearcase "bin"-directory rename "cleardiffmrg.exe" to "cleardiffmrg_orig.exe".
         2. Copy kdiff3.exe into that "bin"-directory and rename it to "cleardiffmrg.exe".
            (Also copy the other files that are needed by KDiff3 there.)
         Now when a file comparison or merge is done by Clearcase then of course KDiff3 will be
         run instead.
         If the commandline contains the option "-directory" then KDiff3 can't do it but will
         run "cleardiffmrg_orig.exe" instead.
Joachim Eibl's avatar
Joachim Eibl committed
130
131
   */

Joachim Eibl's avatar
Joachim Eibl committed
132
133
   // Write all args into a temporary file. Uncomment this for debugging purposes.
   /*
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
134
   FILE* f = fopen(QDir::toNativeSeparators(QDir::homePath()+"//kdiff3_call_args.txt").toLatin1().data(),"w");
Joachim Eibl's avatar
Joachim Eibl committed
135
136
   for(int i=0; i< argc; ++i)
      fprintf(f,"Arg %d: %s\n", i, argv[i]);
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
137
138
   fclose(f);
   
Joachim Eibl's avatar
Joachim Eibl committed
139
140
141
142
   // Call orig cleardiffmrg.exe to see what result it returns.
   int result=0;
   result = ::_spawnvp(_P_WAIT , "C:\\Programme\\Rational\\ClearCase\\bin\\cleardiffmrg.exe", argv );
   fprintf(f,"Result: %d\n", result );
Joachim Eibl's avatar
Joachim Eibl committed
143
   fclose(f);
Joachim Eibl's avatar
Joachim Eibl committed
144
   return result;
Joachim Eibl's avatar
Joachim Eibl committed
145
146
147
148
149
150
151
   */

   // KDiff3 can replace cleardiffmrg from clearcase. But not all functions.
   if ( isOptionUsed( "directory", argc,argv ) )
   {
      return ::_spawnvp(_P_WAIT , "cleardiffmrg_orig", argv );      
   }
Joachim Eibl's avatar
Joachim Eibl committed
152

Joachim Eibl's avatar
Joachim Eibl committed
153
#endif
Joachim Eibl's avatar
Joachim Eibl committed
154
155
156
157
158
#ifdef Q_OS_OS2
   // expand wildcards on the command line
   _wildcard(&argc, &argv);
#endif

Joachim Eibl's avatar
Joachim Eibl committed
159
   //QApplication::setColorSpec( QApplication::ManyColor ); // Grab all 216 colors
Joachim Eibl's avatar
Joachim Eibl committed
160

Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
161
162
163
   const QByteArray& appName = QByteArray("kdiff3");
   const QByteArray& appCatalog = appName;
   const KLocalizedString i18nName = ki18n("kdiff3");
164
165
166
   QByteArray appVersion = QByteArray( VERSION );
   if ( sizeof(void*)==8 )
      appVersion += " (64 bit)";
167
168
   else if ( sizeof(void*)==4 )
       appVersion += " (32 bit)";
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
169
   const KLocalizedString description = ki18n("Tool for Comparison and Merge of Files and Directories");
Joachim Eibl's avatar
Joachim Eibl committed
170
   const KLocalizedString copyright = ki18n("(c) 2002-2012 Joachim Eibl");
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
   const QByteArray& homePage = "http://kdiff3.sourceforge.net/";
   const QByteArray& bugsAddress = "joachim.eibl" "@" "gmx.de";
   KAboutData aboutData( appName, appCatalog, i18nName, 
         appVersion, description, KAboutData::License_GPL_V2, copyright, description, 
         homePage, bugsAddress );

   aboutData.addAuthor(ki18n("Joachim Eibl"), KLocalizedString(), QByteArray("joachim.eibl" "@" "gmx.de"));
   aboutData.addCredit(ki18n("Eike Sauer"), ki18n("Bugfixes, Debian package maintainer") );
   aboutData.addCredit(ki18n("Sebastien Fricker"), ki18n("Windows installer") );
   aboutData.addCredit(ki18n("Stephan Binner"), ki18n("i18n-help"), QByteArray("binner" "@" "kde.org") );
   aboutData.addCredit(ki18n("Stefan Partheymueller"), ki18n("Clipboard-patch" ));
   aboutData.addCredit(ki18n("David Faure"), ki18n("KIO-Help"), QByteArray("faure" "@" "kde.org" ));
   aboutData.addCredit(ki18n("Bernd Gehrmann"), ki18n("Class CvsIgnoreList from Cervisia" ));
   aboutData.addCredit(ki18n("Andre Woebbeking"), ki18n("Class StringMatcher" ));
   aboutData.addCredit(ki18n("Michael Denio"), ki18n("Directory Equality-Coloring patch"));
   aboutData.addCredit(ki18n("Manfred Koehler"), ki18n("Fix for slow startup on Windows"));
   aboutData.addCredit(ki18n("Sergey Zorin"), ki18n("Diff Ext for Windows"));
   aboutData.addCredit(ki18n("Paul Eggert, Mike Haertel, David Hayes, Richard Stallman, Len Tower"), ki18n("GNU-Diffutils"));
   aboutData.addCredit(ki18n("Tino Boellsterling, Timothy Mee"), ki18n("Intensive test, use and feedback"));
   aboutData.addCredit(ki18n("Michael Schmidt"), ki18n("Mac support"));
   aboutData.addCredit(ki18n("Valentin Rusu"), ki18n("KDE4 porting"), QByteArray("kde" "@" "rusu.info"));
   aboutData.addCredit(ki18n("Albert Astals Cid"), ki18n("KDE4 porting"), QByteArray("aacid" "@" "kde.org"));
Joachim Eibl's avatar
Joachim Eibl committed
193
   aboutData.addCredit(ki18n("Silvan Scherrer"), ki18n("OS2 port") );
Joachim Eibl's avatar
Joachim Eibl committed
194
195
   aboutData.addCredit(ki18n("Nigel Stewart"), ki18n("Cygwin support") );
   aboutData.addCredit(ki18n("Maurice van der Pot"), ki18n("Framework for automated tests") );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
196
197

   aboutData.addCredit(ki18n("+ Many thanks to those who reported bugs and contributed ideas!"));
Joachim Eibl's avatar
Joachim Eibl committed
198

Joachim Eibl's avatar
Joachim Eibl committed
199
   KCmdLineArgs::init( argc, argv, &aboutData );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
200
201
202
203
204
205
206
207

   KCmdLineOptions options;
   // ignorable command options
   options.add( "m" ).add( "merge", ki18n("Merge the input."));
   options.add( "b" ).add( "base file", ki18n("Explicit base file. For compatibility with certain tools.") );
   options.add( "o" ).add( "output file", ki18n("Output file. Implies -m. E.g.: -o newfile.txt"));
   options.add( "out file",    ki18n("Output file, again. (For compatibility with certain tools.)") );
   options.add( "auto",        ki18n("No GUI if all conflicts are auto-solvable. (Needs -o file)") );
208
   options.add( "qall",        ki18n("Don't solve conflicts automatically.") );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
209
210
211
212
213
214
215
216
217
218
219
220
221
   options.add( "L1 alias1",   ki18n("Visible name replacement for input file 1 (base).") );
   options.add( "L2 alias2",   ki18n("Visible name replacement for input file 2.") );
   options.add( "L3 alias3",   ki18n("Visible name replacement for input file 3.") );
   options.add( "L" ).add( "fname alias", ki18n("Alternative visible name replacement. Supply this once for every input.") );
   options.add( "cs string",   ki18n("Override a config setting. Use once for every setting. E.g.: --cs \"AutoAdvance=1\"") );
   options.add( "confighelp",  ki18n("Show list of config settings and current values.") );
   options.add( "config file", ki18n("Use a different config file.") );

   // other command options
   options.add( "+[File1]", ki18n("file1 to open (base, if not specified via --base)") );
   options.add( "+[File2]", ki18n("file2 to open") );
   options.add( "+[File3]", ki18n("file3 to open") );

Joachim Eibl's avatar
Joachim Eibl committed
222
   initialiseCmdLineArgs( options );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
223

Joachim Eibl's avatar
Joachim Eibl committed
224
   KCmdLineArgs::addCmdLineOptions( options );
Joachim Eibl's avatar
Joachim Eibl committed
225

Joachim Eibl's avatar
Joachim Eibl committed
226
   KApplication app;
Joachim Eibl's avatar
Joachim Eibl committed
227

Joachim Eibl's avatar
Joachim Eibl committed
228
#ifdef KREPLACEMENTS_H
Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
229
   QString locale;
Joachim Eibl's avatar
Joachim Eibl committed
230

Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
231
   locale = app.config()->readEntry("Language", "Auto");
Joachim Eibl's avatar
Joachim Eibl committed
232
   int spacePos = locale.indexOf(' ');
Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
233
   if (spacePos>0) locale = locale.left(spacePos);
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
234
   ContextFreeTranslator kdiff3Translator( 0 );
Joachim Eibl's avatar
Joachim Eibl committed
235
   QTranslator qtTranslator( 0 );
Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
236
237
238
   if (locale != "en_orig")
   {
      if ( locale == "Auto" || locale.isEmpty() )
Joachim Eibl's avatar
Joachim Eibl committed
239
         locale = locale = QLocale::system().name().left(2);
Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
240
         
Joachim Eibl's avatar
Joachim Eibl committed
241
      QString translationDir = getTranslationDir(locale);
Joachim Eibl's avatar
0.9.86    
Joachim Eibl committed
242
243
244
245
246
247
      kdiff3Translator.load( QString("kdiff3_")+locale, translationDir );
      app.installTranslator( &kdiff3Translator );
      
      qtTranslator.load( QString("qt_")+locale, translationDir );
      app.installTranslator( &qtTranslator );
   }
Joachim Eibl's avatar
Joachim Eibl committed
248
#endif
Joachim Eibl's avatar
Joachim Eibl committed
249

250
#ifndef QT_NO_SESSIONMANAGER
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
251
  if (app.isSessionRestored())
Joachim Eibl's avatar
Joachim Eibl committed
252
253
254
255
  {
     RESTORE(KDiff3Shell);
  }
  else
256
#endif
Joachim Eibl's avatar
Joachim Eibl committed
257
  {
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
258
259
     KDiff3Shell* p = new KDiff3Shell();
     p->show();
260
     p->setWindowState( p->windowState() | Qt::WindowActive ); // Patch for ubuntu: window not active on startup
Joachim Eibl's avatar
Joachim Eibl committed
261
  }
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
262
//app.installEventFilter( new CFilter );
Joachim Eibl's avatar
Joachim Eibl committed
263
  int retVal = app.exec();
264
265
  if (QApplication::clipboard()->text().size() == 0)
     QApplication::clipboard()->clear(); // Patch for Ubuntu: Fix issue with Qt clipboard
Joachim Eibl's avatar
Joachim Eibl committed
266
  return retVal;
Joachim Eibl's avatar
Joachim Eibl committed
267
}
Joachim Eibl's avatar
Joachim Eibl committed
268
269
270

// Suppress warning with --enable-final
#undef VERSION