kateapp.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 #include "kateapp.moc" 00022 00023 #include "katedocmanager.h" 00024 #include "katepluginmanager.h" 00025 #include "kateviewmanager.h" 00026 #include "kateappIface.h" 00027 #include "katesession.h" 00028 #include "katemainwindow.h" 00029 00030 #include "../interfaces/application.h" 00031 00032 #include <tdeversion.h> 00033 #include <tdecmdlineargs.h> 00034 #include <dcopclient.h> 00035 #include <tdeconfig.h> 00036 #include <twin.h> 00037 #include <ktip.h> 00038 #include <kdebug.h> 00039 #include <klibloader.h> 00040 #include <tdemessagebox.h> 00041 #include <tdelocale.h> 00042 #include <ksimpleconfig.h> 00043 #include <tdestartupinfo.h> 00044 00045 #include <tqfile.h> 00046 #include <tqtimer.h> 00047 #include <tqdir.h> 00048 #include <tqtextcodec.h> 00049 00050 #include <stdlib.h> 00051 #include <unistd.h> 00052 #include <sys/types.h> 00053 00054 KateApp::KateApp (TDECmdLineArgs *args) 00055 : TDEApplication () 00056 , m_args (args) 00057 , m_shouldExit (false) 00058 { 00059 // Don't handle DCOP requests yet 00060 dcopClient()->suspend(); 00061 00062 // insert right translations for the katepart 00063 TDEGlobal::locale()->insertCatalogue("katepart"); 00064 00065 // some global default 00066 Kate::Document::setFileChangedDialogsActivated (true); 00067 00068 // application interface 00069 m_application = new Kate::Application (this); 00070 00071 // doc + project man 00072 m_docManager = new KateDocManager (TQT_TQOBJECT(this)); 00073 00074 // init all normal plugins 00075 m_pluginManager = new KatePluginManager (TQT_TQOBJECT(this)); 00076 00077 // session manager up 00078 m_sessionManager = new KateSessionManager (TQT_TQOBJECT(this)); 00079 00080 // application dcop interface 00081 m_obj = new KateAppDCOPIface (this); 00082 00083 kdDebug()<<"Setting KATE_PID: '"<<getpid()<<"'"<<endl; 00084 ::setenv( "KATE_PID", TQString(TQString("%1").arg(getpid())).latin1(), 1 ); 00085 00086 // handle restore different 00087 if (isRestored()) 00088 { 00089 restoreKate (); 00090 } 00091 else 00092 { 00093 // let us handle our command line args and co ;) 00094 // we can exit here if session chooser decides 00095 if (!startupKate ()) 00096 { 00097 m_shouldExit = true; 00098 return; 00099 } 00100 } 00101 00102 // Ok. We are ready for DCOP requests. 00103 dcopClient()->resume(); 00104 } 00105 00106 KateApp::~KateApp () 00107 { 00108 // cu dcop interface 00109 delete m_obj; 00110 00111 // cu plugin manager 00112 delete m_pluginManager; 00113 00114 // delete this now, or we crash 00115 delete m_docManager; 00116 } 00117 00118 KateApp *KateApp::self () 00119 { 00120 return (KateApp *) kapp; 00121 } 00122 00123 Kate::Application *KateApp::application () 00124 { 00125 return m_application; 00126 } 00127 00132 TQString KateApp::kateVersion (bool fullVersion) 00133 { 00134 // return fullVersion ? TQString ("%1.%2.%3").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()).arg(KDE::versionRelease()) 00135 // : TQString ("%1.%2").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()); 00139 return fullVersion ? TQString ("2.5.%1").arg(KDE::versionMajor()) : TQString ("%1.%2").arg(2.5); 00140 } 00141 00142 void KateApp::restoreKate () 00143 { 00144 // restore the nice files ;) we need it 00145 Kate::Document::setOpenErrorDialogsActivated (false); 00146 00147 // activate again correct session!!! 00148 sessionConfig()->setGroup("General"); 00149 TQString lastSession (sessionConfig()->readEntry ("Last Session", "default.katesession")); 00150 sessionManager()->activateSession (new KateSession (sessionManager(), lastSession, ""), false, false, false); 00151 00152 m_docManager->restoreDocumentList (sessionConfig()); 00153 00154 Kate::Document::setOpenErrorDialogsActivated (true); 00155 00156 // restore all windows ;) 00157 for (int n=1; TDEMainWindow::canBeRestored(n); n++) 00158 newMainWindow(sessionConfig(), TQString ("%1").arg(n)); 00159 00160 // oh, no mainwindow, create one, should not happen, but make sure ;) 00161 if (mainWindows() == 0) 00162 newMainWindow (); 00163 00164 // Do not notify about start there: this makes kicker crazy and kate go to a wrong desktop. 00165 // TDEStartupInfo::setNewStartupId( activeMainWindow(), startupId()); 00166 } 00167 00168 bool KateApp::startupKate () 00169 { 00170 // user specified session to open 00171 if (m_args->isSet ("start")) 00172 { 00173 sessionManager()->activateSession (sessionManager()->giveSession (TQString::fromLocal8Bit(m_args->getOption("start"))), false, false); 00174 } 00175 else 00176 { 00177 // let the user choose session if possible 00178 if (!sessionManager()->chooseSession ()) 00179 { 00180 // we will exit kate now, notify the rest of the world we are done 00181 TDEStartupInfo::appStarted (startupId()); 00182 return false; 00183 } 00184 } 00185 00186 // oh, no mainwindow, create one, should not happen, but make sure ;) 00187 if (mainWindows() == 0) 00188 newMainWindow (); 00189 00190 // notify about start 00191 TDEStartupInfo::setNewStartupId( activeMainWindow(), startupId()); 00192 00193 TQTextCodec *codec = m_args->isSet("encoding") ? TQTextCodec::codecForName(m_args->getOption("encoding")) : 0; 00194 00195 bool tempfileSet = TDECmdLineArgs::isTempFileSet(); 00196 00197 Kate::Document::setOpenErrorDialogsActivated (false); 00198 uint id = 0; 00199 for (int z=0; z<m_args->count(); z++) 00200 { 00201 // this file is no local dir, open it, else warn 00202 bool noDir = !m_args->url(z).isLocalFile() || !TQDir (m_args->url(z).path()).exists(); 00203 00204 if (noDir) 00205 { 00206 // open a normal file 00207 if (codec) 00208 id = activeMainWindow()->viewManager()->openURL( m_args->url(z), codec->name(), false, tempfileSet ); 00209 else 00210 id = activeMainWindow()->viewManager()->openURL( m_args->url(z), TQString::null, false, tempfileSet ); 00211 } 00212 else 00213 KMessageBox::sorry( activeMainWindow(), 00214 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(m_args->url(z).url()) ); 00215 } 00216 00217 Kate::Document::setOpenErrorDialogsActivated (true); 00218 00219 // handle stdin input 00220 if( m_args->isSet( "stdin" ) ) 00221 { 00222 TQTextIStream input(stdin); 00223 00224 // set chosen codec 00225 if (codec) 00226 input.setCodec (codec); 00227 00228 TQString line; 00229 TQString text; 00230 00231 do 00232 { 00233 line = input.readLine(); 00234 text.append( line + "\n" ); 00235 } while( !line.isNull() ); 00236 00237 openInput (text); 00238 } 00239 else if ( id ) 00240 activeMainWindow()->viewManager()->activateView( id ); 00241 00242 if ( activeMainWindow()->viewManager()->viewCount () == 0 ) 00243 activeMainWindow()->viewManager()->activateView(m_docManager->firstDocument()->documentNumber()); 00244 00245 int line = 0; 00246 int column = 0; 00247 bool nav = false; 00248 00249 if (m_args->isSet ("line")) 00250 { 00251 line = m_args->getOption ("line").toInt(); 00252 nav = true; 00253 } 00254 00255 if (m_args->isSet ("column")) 00256 { 00257 column = m_args->getOption ("column").toInt(); 00258 nav = true; 00259 } 00260 00261 if (nav) 00262 activeMainWindow()->viewManager()->activeView ()->setCursorPosition (line, column); 00263 00264 // show the nice tips 00265 KTipDialog::showTip(activeMainWindow()); 00266 00267 return true; 00268 } 00269 00270 void KateApp::shutdownKate (KateMainWindow *win) 00271 { 00272 if (!win->queryClose_internal()) 00273 return; 00274 00275 sessionManager()->saveActiveSession(true, true); 00276 00277 // detach the dcopClient 00278 dcopClient()->detach(); 00279 00280 // cu main windows 00281 while (!m_mainWindows.isEmpty()) 00282 delete m_mainWindows[0]; 00283 00284 quit (); 00285 } 00286 00287 KatePluginManager *KateApp::pluginManager() 00288 { 00289 return m_pluginManager; 00290 } 00291 00292 KateDocManager *KateApp::documentManager () 00293 { 00294 return m_docManager; 00295 } 00296 00297 KateSessionManager *KateApp::sessionManager () 00298 { 00299 return m_sessionManager; 00300 } 00301 00302 bool KateApp::openURL (const KURL &url, const TQString &encoding, bool isTempFile) 00303 { 00304 KateMainWindow *mainWindow = activeMainWindow (); 00305 00306 if (!mainWindow) 00307 return false; 00308 00309 TQTextCodec *codec = encoding.isEmpty() ? 0 : TQTextCodec::codecForName(encoding.latin1()); 00310 00311 kdDebug () << "OPEN URL "<< encoding << endl; 00312 00313 // this file is no local dir, open it, else warn 00314 bool noDir = !url.isLocalFile() || !TQDir (url.path()).exists(); 00315 00316 if (noDir) 00317 { 00318 // open a normal file 00319 if (codec) 00320 mainWindow->viewManager()->openURL( url, codec->name(), true, isTempFile ); 00321 else 00322 mainWindow->viewManager()->openURL( url, TQString::null, true, isTempFile ); 00323 } 00324 else 00325 KMessageBox::sorry( mainWindow, 00326 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(url.url()) ); 00327 00328 return true; 00329 } 00330 00331 bool KateApp::setCursor (int line, int column) 00332 { 00333 KateMainWindow *mainWindow = activeMainWindow (); 00334 00335 if (!mainWindow) 00336 return false; 00337 00338 mainWindow->viewManager()->activeView ()->setCursorPosition (line, column); 00339 00340 return true; 00341 } 00342 00343 bool KateApp::openInput (const TQString &text) 00344 { 00345 activeMainWindow()->viewManager()->openURL( "", "", true ); 00346 00347 if (!activeMainWindow()->viewManager()->activeView ()) 00348 return false; 00349 00350 activeMainWindow()->viewManager()->activeView ()->getDoc()->setText (text); 00351 00352 return true; 00353 } 00354 00355 KateMainWindow *KateApp::newMainWindow (TDEConfig *sconfig, const TQString &sgroup) 00356 { 00357 KateMainWindow *mainWindow = new KateMainWindow (sconfig, sgroup); 00358 m_mainWindows.push_back (mainWindow); 00359 00360 if ((mainWindows() > 1) && m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()) 00361 mainWindow->viewManager()->activateView ( m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()->getDoc()->documentNumber() ); 00362 else if ((mainWindows() > 1) && (m_docManager->documents() > 0)) 00363 mainWindow->viewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() ); 00364 else if ((mainWindows() > 1) && (m_docManager->documents() < 1)) 00365 mainWindow->viewManager()->openURL ( KURL() ); 00366 00367 mainWindow->show (); 00368 00369 return mainWindow; 00370 } 00371 00372 void KateApp::removeMainWindow (KateMainWindow *mainWindow) 00373 { 00374 m_mainWindows.remove (mainWindow); 00375 } 00376 00377 KateMainWindow *KateApp::activeMainWindow () 00378 { 00379 if (m_mainWindows.isEmpty()) 00380 return 0; 00381 00382 int n = m_mainWindows.findIndex ((KateMainWindow *)activeWindow()); 00383 00384 if (n < 0) 00385 n=0; 00386 00387 return m_mainWindows[n]; 00388 } 00389 00390 uint KateApp::mainWindows () const 00391 { 00392 return m_mainWindows.size(); 00393 } 00394 00395 KateMainWindow *KateApp::mainWindow (uint n) 00396 { 00397 if (n < m_mainWindows.size()) 00398 return m_mainWindows[n]; 00399 00400 return 0; 00401 } 00402 00403 // kate: space-indent on; indent-width 2; replace-tabs on;