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 = KateSessionManager::self(); 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 delete m_obj; // cu dcop interface 00109 delete m_pluginManager; // cu plugin manager 00110 delete m_sessionManager; // delete session manager 00111 delete m_docManager; // delete document manager. Do this now, or we crash 00112 } 00113 00114 KateApp *KateApp::self () 00115 { 00116 return (KateApp *) kapp; 00117 } 00118 00119 Kate::Application *KateApp::application () 00120 { 00121 return m_application; 00122 } 00123 00128 TQString KateApp::kateVersion (bool fullVersion) 00129 { 00130 // return fullVersion ? TQString ("%1.%2.%3").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()).arg(KDE::versionRelease()) 00131 // : TQString ("%1.%2").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()); 00135 return fullVersion ? TQString ("2.5.%1").arg(KDE::versionMajor()) : TQString ("%1.%2").arg(2.5); 00136 } 00137 00138 void KateApp::restoreKate() 00139 { 00140 // restore the nice files ;) we need it 00141 Kate::Document::setOpenErrorDialogsActivated(false); 00142 00143 // restore last session 00144 sessionManager()->restoreLastSession(); 00145 m_docManager->restoreDocumentList(sessionConfig()); 00146 00147 Kate::Document::setOpenErrorDialogsActivated(true); 00148 00149 // no mainwindow, create one, should not happen, but make sure ;) 00150 if (mainWindows() == 0) 00151 newMainWindow(); 00152 00153 // Do not notify about start there: this makes kicker crazy and kate go to a wrong desktop. 00154 // TDEStartupInfo::setNewStartupId( activeMainWindow(), startupId()); 00155 } 00156 00157 bool KateApp::startupKate() 00158 { 00159 if (m_args->isSet("start")) 00160 { 00161 // the user has specified the session to open. If the session does not exist, 00162 // a new session with the specified name will be created 00163 TQCString sessName = m_args->getOption("start"); 00164 int sessId = sessionManager()->getSessionIdFromName(sessName); 00165 if (sessId != KateSessionManager::INVALID_SESSION) 00166 { 00167 sessionManager()->activateSession(sessId); 00168 } 00169 else 00170 { 00171 sessionManager()->newSession(sessName); 00172 } 00173 } 00174 else 00175 { 00176 // check Kate session startup options 00177 int startupOption = sessionManager()->getStartupOption(); 00178 if (startupOption == KateSessionManager::STARTUP_NEW) 00179 { 00180 sessionManager()->newSession(); 00181 } 00182 else if (startupOption == KateSessionManager::STARTUP_LAST) 00183 { 00184 sessionManager()->restoreLastSession(); 00185 } 00186 else // startupOption == KateSessionManager::STARTUP_MANUAL 00187 { 00188 KateSessionChooser *chooser = new KateSessionChooser(NULL); 00189 int result = chooser->exec(); 00190 switch (result) 00191 { 00192 case KateSessionChooser::RESULT_OPEN_NEW: 00193 sessionManager()->newSession(); 00194 break; 00195 00196 case KateSessionChooser::RESULT_OPEN_EXISTING: 00197 if (!m_sessionManager->activateSession(chooser->getSelectedSessionId())) 00198 { 00199 // Open a new session in case of error 00200 sessionManager()->newSession(); 00201 } 00202 break; 00203 00204 default: // KateSessionChooser::RESULT_QUIT_KATE: 00205 // Kate will exit now and notify it is done 00206 TDEStartupInfo::appStarted(startupId()); 00207 return false; 00208 break; 00209 } 00210 delete chooser; 00211 } 00212 } 00213 00214 // oh, no mainwindow, create one, should not happen, but make sure ;) 00215 if (mainWindows() == 0) 00216 newMainWindow (); 00217 00218 // notify about start 00219 TDEStartupInfo::setNewStartupId( activeMainWindow(), startupId()); 00220 00221 TQTextCodec *codec = m_args->isSet("encoding") ? TQTextCodec::codecForName(m_args->getOption("encoding")) : 0; 00222 00223 bool tempfileSet = TDECmdLineArgs::isTempFileSet(); 00224 00225 Kate::Document::setOpenErrorDialogsActivated (false); 00226 uint id = 0; 00227 for (int z=0; z<m_args->count(); z++) 00228 { 00229 // this file is no local dir, open it, else warn 00230 bool noDir = !m_args->url(z).isLocalFile() || !TQDir (m_args->url(z).path()).exists(); 00231 00232 if (noDir) 00233 { 00234 // open a normal file 00235 if (codec) 00236 id = activeMainWindow()->viewManager()->openURL( m_args->url(z), codec->name(), false, tempfileSet ); 00237 else 00238 id = activeMainWindow()->viewManager()->openURL( m_args->url(z), TQString::null, false, tempfileSet ); 00239 } 00240 else 00241 KMessageBox::sorry( activeMainWindow(), 00242 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(m_args->url(z).url()) ); 00243 } 00244 00245 Kate::Document::setOpenErrorDialogsActivated (true); 00246 00247 // handle stdin input 00248 if( m_args->isSet( "stdin" ) ) 00249 { 00250 TQTextIStream input(stdin); 00251 00252 // set chosen codec 00253 if (codec) 00254 input.setCodec (codec); 00255 00256 TQString line; 00257 TQString text; 00258 00259 do 00260 { 00261 line = input.readLine(); 00262 text.append( line + "\n" ); 00263 } while( !line.isNull() ); 00264 00265 openInput (text); 00266 } 00267 else if ( id ) 00268 activeMainWindow()->viewManager()->activateView( id ); 00269 00270 if ( activeMainWindow()->viewManager()->viewCount () == 0 ) 00271 activeMainWindow()->viewManager()->activateView(m_docManager->firstDocument()->documentNumber()); 00272 00273 int line = 0; 00274 int column = 0; 00275 bool nav = false; 00276 00277 if (m_args->isSet ("line")) 00278 { 00279 line = m_args->getOption ("line").toInt(); 00280 nav = true; 00281 } 00282 00283 if (m_args->isSet ("column")) 00284 { 00285 column = m_args->getOption ("column").toInt(); 00286 nav = true; 00287 } 00288 00289 if (nav) 00290 activeMainWindow()->viewManager()->activeView ()->setCursorPosition (line, column); 00291 00292 // show the nice tips 00293 KTipDialog::showTip(activeMainWindow()); 00294 00295 return true; 00296 } 00297 00298 void KateApp::shutdownKate(KateMainWindow *win) 00299 { 00300 if (!win->queryClose_internal()) 00301 return; 00302 00303 // detach the dcopClient 00304 dcopClient()->detach(); 00305 00306 // cu main windows 00307 while (!m_mainWindows.isEmpty()) 00308 delete m_mainWindows[0]; 00309 00310 quit(); 00311 } 00312 00313 bool KateApp::query_session_close() 00314 { 00315 bool saveSessions = false; 00316 int switchOption = m_sessionManager->getSwitchOption(); 00317 if (switchOption == KateSessionManager::SWITCH_SAVE) 00318 { 00319 saveSessions = true; 00320 } 00321 else if (switchOption == KateSessionManager::SWITCH_ASK) 00322 { 00323 KDialogBase *dlg = new KDialogBase(i18n("Save Sessions"), 00324 KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel, 00325 KDialogBase::Cancel, KDialogBase::Cancel, NULL, NULL, true, false, 00326 KStdGuiItem::save(), KStdGuiItem::del(), KStdGuiItem::cancel()); 00327 bool dontAgain = false; 00328 int res = KMessageBox::createKMessageBox(dlg, TQMessageBox::Warning, 00329 i18n("<p>Do you want to save the existing sessions?<p>!!NOTE!!" 00330 "<p>All existing sessions will be removed " 00331 "if you choose \"Delete\""), TQStringList(), 00332 i18n("Do not ask again"), &dontAgain, KMessageBox::Notify); 00333 if (res == KDialogBase::Cancel) 00334 { 00335 return false; 00336 } 00337 if (dontAgain) 00338 { 00339 if (res == KDialogBase::No) 00340 { 00341 m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_DISCARD); 00342 } 00343 else 00344 { 00345 m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_SAVE); 00346 } 00347 } 00348 if (res == KDialogBase::Yes) 00349 { 00350 saveSessions = true; 00351 } 00352 } 00353 00354 if (saveSessions) 00355 { 00356 m_sessionManager->saveActiveSession(); 00357 } 00358 m_sessionManager->saveConfig(saveSessions); 00359 return true; 00360 } 00361 00362 void KateApp::reparse_config() 00363 { 00364 emit optionsChanged(); 00365 } 00366 00367 KatePluginManager *KateApp::pluginManager() 00368 { 00369 return m_pluginManager; 00370 } 00371 00372 KateDocManager *KateApp::documentManager () 00373 { 00374 return m_docManager; 00375 } 00376 00377 KateSessionManager* KateApp::sessionManager() 00378 { 00379 return m_sessionManager; 00380 } 00381 00382 bool KateApp::openURL (const KURL &url, const TQString &encoding, bool isTempFile) 00383 { 00384 KateMainWindow *mainWindow = activeMainWindow (); 00385 00386 if (!mainWindow) 00387 return false; 00388 00389 TQTextCodec *codec = encoding.isEmpty() ? 0 : TQTextCodec::codecForName(encoding.latin1()); 00390 00391 kdDebug () << "OPEN URL "<< encoding << endl; 00392 00393 // this file is no local dir, open it, else warn 00394 bool noDir = !url.isLocalFile() || !TQDir (url.path()).exists(); 00395 00396 if (noDir) 00397 { 00398 // open a normal file 00399 if (codec) 00400 mainWindow->viewManager()->openURL( url, codec->name(), true, isTempFile ); 00401 else 00402 mainWindow->viewManager()->openURL( url, TQString::null, true, isTempFile ); 00403 } 00404 else 00405 KMessageBox::sorry( mainWindow, 00406 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(url.url()) ); 00407 00408 return true; 00409 } 00410 00411 bool KateApp::setCursor (int line, int column) 00412 { 00413 KateMainWindow *mainWindow = activeMainWindow (); 00414 00415 if (!mainWindow) 00416 return false; 00417 00418 mainWindow->viewManager()->activeView ()->setCursorPosition (line, column); 00419 00420 return true; 00421 } 00422 00423 bool KateApp::openInput (const TQString &text) 00424 { 00425 activeMainWindow()->viewManager()->openURL( "", "", true ); 00426 00427 if (!activeMainWindow()->viewManager()->activeView ()) 00428 return false; 00429 00430 activeMainWindow()->viewManager()->activeView ()->getDoc()->setText (text); 00431 00432 return true; 00433 } 00434 00435 KateMainWindow *KateApp::newMainWindow (TDEConfig *sconfig, const TQString &sgroup) 00436 { 00437 KateMainWindow *mainWindow = new KateMainWindow (sconfig, sgroup); 00438 m_mainWindows.push_back (mainWindow); 00439 00440 if ((mainWindows() > 1) && m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()) 00441 mainWindow->viewManager()->activateView ( m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()->getDoc()->documentNumber() ); 00442 else if ((mainWindows() > 1) && (m_docManager->documents() > 0)) 00443 mainWindow->viewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() ); 00444 else if ((mainWindows() > 1) && (m_docManager->documents() < 1)) 00445 mainWindow->viewManager()->openURL ( KURL() ); 00446 00447 mainWindow->show (); 00448 00449 return mainWindow; 00450 } 00451 00452 void KateApp::removeMainWindow (KateMainWindow *mainWindow) 00453 { 00454 m_mainWindows.remove (mainWindow); 00455 } 00456 00457 KateMainWindow *KateApp::activeMainWindow () 00458 { 00459 if (m_mainWindows.isEmpty()) 00460 return 0; 00461 00462 int n = m_mainWindows.findIndex ((KateMainWindow *)activeWindow()); 00463 00464 if (n < 0) 00465 n=0; 00466 00467 return m_mainWindows[n]; 00468 } 00469 00470 uint KateApp::mainWindows () const 00471 { 00472 return m_mainWindows.size(); 00473 } 00474 00475 KateMainWindow *KateApp::mainWindow (uint n) 00476 { 00477 if (n < m_mainWindows.size()) 00478 return m_mainWindows[n]; 00479 00480 return 0; 00481 } 00482 00483 // kate: space-indent on; indent-width 2; replace-tabs on;
Trinity API Reference