katemain.cpp
00001 /* This file is part of the KDE project 00002 Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> 00003 Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kateapp.h" 00021 00022 #include <kstandarddirs.h> 00023 #include <klocale.h> 00024 #include <kcmdlineargs.h> 00025 #include <kaboutdata.h> 00026 #include <kglobal.h> 00027 #include <kconfig.h> 00028 #include <kinstance.h> 00029 #include <kstartupinfo.h> 00030 #include <dcopclient.h> 00031 #include <dcopref.h> 00032 #include <kdebug.h> 00033 00034 #include <tqtextcodec.h> 00035 00036 #include <stdlib.h> 00037 00038 static KCmdLineOptions options[] = 00039 { 00040 { "s", 0 , 0 }, 00041 { "start <name>", I18N_NOOP("Start Kate with a given session"), 0 }, 00042 { "u", 0, 0 }, 00043 { "use", I18N_NOOP("Use a already running kate instance (if possible)"), 0 }, 00044 { "p", 0, 0 }, 00045 { "pid <pid>", I18N_NOOP("Only try to reuse kate instance with this pid"), 0 }, 00046 { "e", 0, 0 }, 00047 { "encoding <name>", I18N_NOOP("Set encoding for the file to open"), 0 }, 00048 { "l", 0, 0 }, 00049 { "line <line>", I18N_NOOP("Navigate to this line"), 0 }, 00050 { "c", 0, 0 }, 00051 { "column <column>", I18N_NOOP("Navigate to this column"), 0 }, 00052 { "i", 0, 0 }, 00053 { "stdin", I18N_NOOP("Read the contents of stdin"), 0 }, 00054 { "+[URL]", I18N_NOOP("Document to open"), 0 }, 00055 KCmdLineLastOption 00056 }; 00057 00058 extern "C" KDE_EXPORT int kdemain( int argc, char **argv ) 00059 { 00060 // here we go, construct the Kate version 00061 TQString kateVersion = KateApp::kateVersion(); 00062 00063 KAboutData aboutData ("kate", I18N_NOOP("Kate"), kateVersion.latin1(), 00064 I18N_NOOP( "Kate - Advanced Text Editor" ), KAboutData::License_LGPL_V2, 00065 I18N_NOOP( "(c) 2000-2005 The Kate Authors" ), 0, "http://kate.kde.org"); 00066 00067 aboutData.addAuthor ("Christoph Cullmann", I18N_NOOP("Maintainer"), "cullmann@kde.org", "http://www.babylon2k.de"); 00068 aboutData.addAuthor ("Anders Lund", I18N_NOOP("Core Developer"), "anders@alweb.dk", "http://www.alweb.dk"); 00069 aboutData.addAuthor ("Joseph Wenninger", I18N_NOOP("Core Developer"), "jowenn@kde.org","http://stud3.tuwien.ac.at/~e9925371"); 00070 aboutData.addAuthor ("Hamish Rodda",I18N_NOOP("Core Developer"), "rodda@kde.org"); 00071 aboutData.addAuthor ("Waldo Bastian", I18N_NOOP( "The cool buffersystem" ), "bastian@kde.org" ); 00072 aboutData.addAuthor ("Charles Samuels", I18N_NOOP("The Editing Commands"), "charles@kde.org"); 00073 aboutData.addAuthor ("Matt Newell", I18N_NOOP("Testing, ..."), "newellm@proaxis.com"); 00074 aboutData.addAuthor ("Michael Bartl", I18N_NOOP("Former Core Developer"), "michael.bartl1@chello.at"); 00075 aboutData.addAuthor ("Michael McCallum", I18N_NOOP("Core Developer"), "gholam@xtra.co.nz"); 00076 aboutData.addAuthor ("Jochen Wilhemly", I18N_NOOP( "KWrite Author" ), "digisnap@cs.tu-berlin.de" ); 00077 aboutData.addAuthor ("Michael Koch",I18N_NOOP("KWrite port to KParts"), "koch@kde.org"); 00078 aboutData.addAuthor ("Christian Gebauer", 0, "gebauer@kde.org" ); 00079 aboutData.addAuthor ("Simon Hausmann", 0, "hausmann@kde.org" ); 00080 aboutData.addAuthor ("Glen Parker",I18N_NOOP("KWrite Undo History, Kspell integration"), "glenebob@nwlink.com"); 00081 aboutData.addAuthor ("Scott Manson",I18N_NOOP("KWrite XML Syntax highlighting support"), "sdmanson@alltel.net"); 00082 aboutData.addAuthor ("John Firebaugh",I18N_NOOP("Patches and more"), "jfirebaugh@kde.org"); 00083 aboutData.addAuthor ("Dominik Haumann", I18N_NOOP("Developer & Highlight wizard"), "dhdev@gmx.de"); 00084 00085 aboutData.addCredit ("Matteo Merli",I18N_NOOP("Highlighting for RPM Spec-Files, Perl, Diff and more"), "merlim@libero.it"); 00086 aboutData.addCredit ("Rocky Scaletta",I18N_NOOP("Highlighting for VHDL"), "rocky@purdue.edu"); 00087 aboutData.addCredit ("Yury Lebedev",I18N_NOOP("Highlighting for SQL"),""); 00088 aboutData.addCredit ("Chris Ross",I18N_NOOP("Highlighting for Ferite"),""); 00089 aboutData.addCredit ("Nick Roux",I18N_NOOP("Highlighting for ILERPG"),""); 00090 aboutData.addCredit ("Carsten Niehaus", I18N_NOOP("Highlighting for LaTeX"),""); 00091 aboutData.addCredit ("Per Wigren", I18N_NOOP("Highlighting for Makefiles, Python"),""); 00092 aboutData.addCredit ("Jan Fritz", I18N_NOOP("Highlighting for Python"),""); 00093 aboutData.addCredit ("Daniel Naber","",""); 00094 aboutData.addCredit ("Roland Pabel",I18N_NOOP("Highlighting for Scheme"),""); 00095 aboutData.addCredit ("Cristi Dumitrescu",I18N_NOOP("PHP Keyword/Datatype list"),""); 00096 aboutData.addCredit ("Carsten Pfeiffer", I18N_NOOP("Very nice help"), ""); 00097 aboutData.addCredit (I18N_NOOP("All people who have contributed and I have forgotten to mention"),"",""); 00098 00099 aboutData.setTranslator(I18N_NOOP2("NAME OF TRANSLATORS","Your names"), I18N_NOOP2("EMAIL OF TRANSLATORS","Your emails")); 00100 00101 // command line args init and co 00102 KCmdLineArgs::init (argc, argv, &aboutData); 00103 KCmdLineArgs::addCmdLineOptions (options); 00104 KCmdLineArgs::addTempFileOption(); 00105 KateApp::addCmdLineOptions (); 00106 00107 // get our command line args ;) 00108 KCmdLineArgs* args = KCmdLineArgs::parsedArgs(); 00109 00110 // now, first try to contact running kate instance if needed 00111 if ( args->isSet("use") || (::getenv("KATE_PID")!=0) ) 00112 { 00113 DCOPClient client; 00114 client.attach (); 00115 00116 // get all attached clients ;) 00117 QCStringList allClients = client.registeredApplications(); 00118 00119 // search for a kate app client, use the first found 00120 TQCString kateApp; 00121 00122 if ( args->isSet("start") ) 00123 { 00124 for (unsigned int i=0; i < allClients.count(); i++) 00125 { 00126 if (allClients[i] == "kate" || allClients[i].left(5) == "kate-") 00127 { 00128 DCOPRef ref( allClients[i], "KateApplication" ); 00129 TQString s = ref.call( "session" ); 00130 if ( TQString(args->getOption("start")) == s ) 00131 { 00132 kateApp = allClients[i]; 00133 break; 00134 } 00135 } 00136 } 00137 } 00138 else if ( (args->isSet("pid")) || (::getenv("KATE_PID") !=0 ) ) 00139 { 00140 TQCString tryApp; 00141 if ( args->isSet("pid") ) 00142 tryApp = args->getOption("pid"); 00143 else 00144 tryApp = ::getenv("KATE_PID"); 00145 00146 if ( client.isApplicationRegistered( tryApp.prepend("kate-") ) ) 00147 kateApp = tryApp; 00148 } 00149 else 00150 { 00151 for (unsigned int i=0; i < allClients.count(); ++i) 00152 { 00153 if (allClients[i] == "kate" || allClients[i].left(5) == "kate-") 00154 { 00155 kateApp = allClients[i]; 00156 break; 00157 } 00158 } 00159 } 00160 00161 // found a matching kate client ;) 00162 if (!kateApp.isEmpty()) 00163 { 00164 kdDebug () << "kate app: " << kateApp << endl; 00165 // make kdeinit happy 00166 client.registerAs( "kate" ); 00167 00168 DCOPRef kRef (kateApp, "KateApplication"); 00169 00170 if (args->isSet ("start")) 00171 kRef.call( "activateSession", TQString (args->getOption("start")) ); 00172 00173 TQString enc = args->isSet("encoding") ? args->getOption("encoding") : TQCString(""); 00174 00175 bool tempfileSet = KCmdLineArgs::isTempFileSet(); 00176 00177 for (int z=0; z<args->count(); z++) 00178 kRef.call( "openURL", args->url(z), enc, tempfileSet ); 00179 00180 if( args->isSet( "stdin" ) ) 00181 { 00182 TQTextIStream input(stdin); 00183 00184 // set chosen codec 00185 TQTextCodec *codec = args->isSet("encoding") ? TQTextCodec::codecForName(args->getOption("encoding")) : 0; 00186 00187 if (codec) 00188 input.setCodec (codec); 00189 00190 TQString line; 00191 TQString text; 00192 00193 do 00194 { 00195 line = input.readLine(); 00196 text.append( line + "\n" ); 00197 } while( !line.isNull() ); 00198 00199 kRef.call( "openInput", text ); 00200 } 00201 00202 int line = 0; 00203 int column = 0; 00204 bool nav = false; 00205 00206 if (args->isSet ("line")) 00207 { 00208 line = args->getOption ("line").toInt(); 00209 nav = true; 00210 } 00211 00212 if (args->isSet ("column")) 00213 { 00214 column = args->getOption ("column").toInt(); 00215 nav = true; 00216 } 00217 00218 if (nav) 00219 kRef.call( "setCursor", line, column ); 00220 00221 // since the user tried to open a document, let us assume [s]he 00222 // wants to see that document. 00223 // ### what to do about the infamous focus stealing prevention? 00224 uint mwn = kRef.call("activeMainWindowNumber"); 00225 TQCString smwn; 00226 DCOPRef wRef( kateApp, TQCString( "__KateMainWindow#") + smwn.setNum(mwn) ); 00227 if ( wRef.call("minimized") ) 00228 { 00229 if ( wRef.call( "maximized" ) ) 00230 wRef.call( "maximize" ); 00231 else 00232 wRef.call("restore"); 00233 } 00234 // The --use parameter forces Kate to use a single session window to host 00235 // all opened files. Generally, that parameter works. Yet when a file is 00236 // opened into Kate externally, such as from Konqueror, Kate will pop 00237 // forward (raise) but not grab the focus (show). The following snippet 00238 // resolves that problem. For whatever reason the hide call is needed 00239 // before the raise and show otherwise the snippet fails. Some extra glue 00240 // might be needed. With Kate open: 00241 // "Right-click" on the title bar 00242 // Select Advanced 00243 // Select Special Application Settings (Not Special Window Settings) 00244 // Select the Workarounds tab 00245 // Enable the Focus stealing prevention check box 00246 // Select the Force option 00247 // Select the None option 00248 00249 wRef.call( "hide" ); 00250 wRef.call( "raise" ); 00251 wRef.call( "show" ); 00252 00253 // stop startup notification 00254 KStartupInfo::appStarted( ); 00255 00256 return 0; 00257 } 00258 } 00259 00260 // construct the real kate app object ;) 00261 KateApp app (args); 00262 00263 // app execution should already end :) 00264 if (app.shouldExit()) 00265 { 00266 return 0; 00267 } 00268 00269 // execute ourself ;) 00270 return app.exec(); 00271 } 00272 00273 // kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;