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 <kdeversion.h> 00033 #include <kcmdlineargs.h> 00034 #include <dcopclient.h> 00035 #include <kconfig.h> 00036 #include <kwin.h> 00037 #include <ktip.h> 00038 #include <kdebug.h> 00039 #include <klibloader.h> 00040 #include <kmessagebox.h> 00041 #include <klocale.h> 00042 #include <ksimpleconfig.h> 00043 #include <kstartupinfo.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 (KCmdLineArgs *args) 00055 : KApplication () 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 KGlobal::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()); 00136 } 00137 00138 void KateApp::restoreKate () 00139 { 00140 // restore the nice files ;) we need it 00141 Kate::Document::setOpenErrorDialogsActivated (false); 00142 00143 // activate again correct session!!! 00144 sessionConfig()->setGroup("General"); 00145 TQString lastSession (sessionConfig()->readEntry ("Last Session", "default.katesession")); 00146 sessionManager()->activateSession (new KateSession (sessionManager(), lastSession, ""), false, false, false); 00147 00148 m_docManager->restoreDocumentList (sessionConfig()); 00149 00150 Kate::Document::setOpenErrorDialogsActivated (true); 00151 00152 // restore all windows ;) 00153 for (int n=1; KMainWindow::canBeRestored(n); n++) 00154 newMainWindow(sessionConfig(), TQString ("%1").arg(n)); 00155 00156 // oh, no mainwindow, create one, should not happen, but make sure ;) 00157 if (mainWindows() == 0) 00158 newMainWindow (); 00159 00160 // Do not notify about start there: this makes kicker crazy and kate go to a wrong desktop. 00161 // KStartupInfo::setNewStartupId( activeMainWindow(), startupId()); 00162 } 00163 00164 bool KateApp::startupKate () 00165 { 00166 // user specified session to open 00167 if (m_args->isSet ("start")) 00168 { 00169 sessionManager()->activateSession (sessionManager()->giveSession (TQString::fromLocal8Bit(m_args->getOption("start"))), false, false); 00170 } 00171 else 00172 { 00173 // let the user choose session if possible 00174 if (!sessionManager()->chooseSession ()) 00175 { 00176 // we will exit kate now, notify the rest of the world we are done 00177 KStartupInfo::appStarted (startupId()); 00178 return false; 00179 } 00180 } 00181 00182 // oh, no mainwindow, create one, should not happen, but make sure ;) 00183 if (mainWindows() == 0) 00184 newMainWindow (); 00185 00186 // notify about start 00187 KStartupInfo::setNewStartupId( activeMainWindow(), startupId()); 00188 00189 TQTextCodec *codec = m_args->isSet("encoding") ? TQTextCodec::codecForName(m_args->getOption("encoding")) : 0; 00190 00191 bool tempfileSet = KCmdLineArgs::isTempFileSet(); 00192 00193 Kate::Document::setOpenErrorDialogsActivated (false); 00194 uint id = 0; 00195 for (int z=0; z<m_args->count(); z++) 00196 { 00197 // this file is no local dir, open it, else warn 00198 bool noDir = !m_args->url(z).isLocalFile() || !TQDir (m_args->url(z).path()).exists(); 00199 00200 if (noDir) 00201 { 00202 // open a normal file 00203 if (codec) 00204 id = activeMainWindow()->viewManager()->openURL( m_args->url(z), codec->name(), false, tempfileSet ); 00205 else 00206 id = activeMainWindow()->viewManager()->openURL( m_args->url(z), TQString::null, false, tempfileSet ); 00207 } 00208 else 00209 KMessageBox::sorry( activeMainWindow(), 00210 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(m_args->url(z).url()) ); 00211 } 00212 00213 Kate::Document::setOpenErrorDialogsActivated (true); 00214 00215 // handle stdin input 00216 if( m_args->isSet( "stdin" ) ) 00217 { 00218 TQTextIStream input(stdin); 00219 00220 // set chosen codec 00221 if (codec) 00222 input.setCodec (codec); 00223 00224 TQString line; 00225 TQString text; 00226 00227 do 00228 { 00229 line = input.readLine(); 00230 text.append( line + "\n" ); 00231 } while( !line.isNull() ); 00232 00233 openInput (text); 00234 } 00235 else if ( id ) 00236 activeMainWindow()->viewManager()->activateView( id ); 00237 00238 if ( activeMainWindow()->viewManager()->viewCount () == 0 ) 00239 activeMainWindow()->viewManager()->activateView(m_docManager->firstDocument()->documentNumber()); 00240 00241 int line = 0; 00242 int column = 0; 00243 bool nav = false; 00244 00245 if (m_args->isSet ("line")) 00246 { 00247 line = m_args->getOption ("line").toInt(); 00248 nav = true; 00249 } 00250 00251 if (m_args->isSet ("column")) 00252 { 00253 column = m_args->getOption ("column").toInt(); 00254 nav = true; 00255 } 00256 00257 if (nav) 00258 activeMainWindow()->viewManager()->activeView ()->setCursorPosition (line, column); 00259 00260 // show the nice tips 00261 KTipDialog::showTip(activeMainWindow()); 00262 00263 return true; 00264 } 00265 00266 void KateApp::shutdownKate (KateMainWindow *win) 00267 { 00268 if (!win->queryClose_internal()) 00269 return; 00270 00271 sessionManager()->saveActiveSession(true, true); 00272 00273 // detach the dcopClient 00274 dcopClient()->detach(); 00275 00276 // cu main windows 00277 while (!m_mainWindows.isEmpty()) 00278 delete m_mainWindows[0]; 00279 00280 quit (); 00281 } 00282 00283 KatePluginManager *KateApp::pluginManager() 00284 { 00285 return m_pluginManager; 00286 } 00287 00288 KateDocManager *KateApp::documentManager () 00289 { 00290 return m_docManager; 00291 } 00292 00293 KateSessionManager *KateApp::sessionManager () 00294 { 00295 return m_sessionManager; 00296 } 00297 00298 bool KateApp::openURL (const KURL &url, const TQString &encoding, bool isTempFile) 00299 { 00300 KateMainWindow *mainWindow = activeMainWindow (); 00301 00302 if (!mainWindow) 00303 return false; 00304 00305 TQTextCodec *codec = encoding.isEmpty() ? 0 : TQTextCodec::codecForName(encoding.latin1()); 00306 00307 kdDebug () << "OPEN URL "<< encoding << endl; 00308 00309 // this file is no local dir, open it, else warn 00310 bool noDir = !url.isLocalFile() || !TQDir (url.path()).exists(); 00311 00312 if (noDir) 00313 { 00314 // open a normal file 00315 if (codec) 00316 mainWindow->viewManager()->openURL( url, codec->name(), true, isTempFile ); 00317 else 00318 mainWindow->viewManager()->openURL( url, TQString::null, true, isTempFile ); 00319 } 00320 else 00321 KMessageBox::sorry( mainWindow, 00322 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(url.url()) ); 00323 00324 return true; 00325 } 00326 00327 bool KateApp::setCursor (int line, int column) 00328 { 00329 KateMainWindow *mainWindow = activeMainWindow (); 00330 00331 if (!mainWindow) 00332 return false; 00333 00334 mainWindow->viewManager()->activeView ()->setCursorPosition (line, column); 00335 00336 return true; 00337 } 00338 00339 bool KateApp::openInput (const TQString &text) 00340 { 00341 activeMainWindow()->viewManager()->openURL( "", "", true ); 00342 00343 if (!activeMainWindow()->viewManager()->activeView ()) 00344 return false; 00345 00346 activeMainWindow()->viewManager()->activeView ()->getDoc()->setText (text); 00347 00348 return true; 00349 } 00350 00351 KateMainWindow *KateApp::newMainWindow (KConfig *sconfig, const TQString &sgroup) 00352 { 00353 KateMainWindow *mainWindow = new KateMainWindow (sconfig, sgroup); 00354 m_mainWindows.push_back (mainWindow); 00355 00356 if ((mainWindows() > 1) && m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()) 00357 mainWindow->viewManager()->activateView ( m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()->getDoc()->documentNumber() ); 00358 else if ((mainWindows() > 1) && (m_docManager->documents() > 0)) 00359 mainWindow->viewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() ); 00360 else if ((mainWindows() > 1) && (m_docManager->documents() < 1)) 00361 mainWindow->viewManager()->openURL ( KURL() ); 00362 00363 mainWindow->show (); 00364 00365 return mainWindow; 00366 } 00367 00368 void KateApp::removeMainWindow (KateMainWindow *mainWindow) 00369 { 00370 m_mainWindows.remove (mainWindow); 00371 } 00372 00373 KateMainWindow *KateApp::activeMainWindow () 00374 { 00375 if (m_mainWindows.isEmpty()) 00376 return 0; 00377 00378 int n = m_mainWindows.findIndex ((KateMainWindow *)activeWindow()); 00379 00380 if (n < 0) 00381 n=0; 00382 00383 return m_mainWindows[n]; 00384 } 00385 00386 uint KateApp::mainWindows () const 00387 { 00388 return m_mainWindows.size(); 00389 } 00390 00391 KateMainWindow *KateApp::mainWindow (uint n) 00392 { 00393 if (n < m_mainWindows.size()) 00394 return m_mainWindows[n]; 00395 00396 return 0; 00397 } 00398 00399 // kate: space-indent on; indent-width 2; replace-tabs on;