kateapp.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00028 #include <kcmdlineargs.h>
00029 #include <dcopclient.h>
00030 #include <kconfig.h>
00031 #include <kwin.h>
00032 #include <ktip.h>
00033 #include <kdebug.h>
00034 #include <klibloader.h>
00035 #include <kmessagebox.h>
00036 #include <klocale.h>
00037 #include <ksimpleconfig.h>
00038 #include <kstartupinfo.h>
00039
00040 #include <qfile.h>
00041 #include <qtimer.h>
00042 #include <qdir.h>
00043 #include <qtextcodec.h>
00044
00045 KConfig *KateApp::m_sessionConfig = 0;
00046
00047 KateApp::KateApp (bool forcedNewProcess, bool oldState)
00048 : KUniqueApplication (true,true,true)
00049 , m_firstStart (true)
00050 , m_initPlugin (0)
00051 , m_doNotInitialize (0)
00052 {
00053
00054 if (isRestored())
00055 {
00056 m_sessionConfig = sessionConfig ();
00057 m_sessionConfigDelete = false;
00058 }
00059 else
00060 {
00061 m_sessionConfig = new KSimpleConfig ("katesessionrc", false);
00062 m_sessionConfigDelete = true;
00063 }
00064
00065
00066 kapp->dcopClient()->suspend();
00067
00068 m_mainWindows.setAutoDelete (false);
00069
00070
00071
00072 if (forcedNewProcess)
00073 {
00074 config()->setGroup("KDE");
00075 config()->writeEntry("MultipleInstances",oldState);
00076 config()->sync();
00077 }
00078
00079
00080 m_application = new Kate::Application (this);
00081
00082
00083 m_initPluginManager = new Kate::InitPluginManager (this);
00084
00085
00086 m_obj = new KateAppDCOPIface (this);
00087
00088
00089 KGlobal::locale()->insertCatalogue("katepart");
00090
00091
00092 m_docManager = new KateDocManager (this);
00093 m_projectManager = new KateProjectManager (this);
00094
00095
00096 m_pluginManager = new KatePluginManager (this);
00097
00098 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00099
00100 if (args->isSet("initplugin"))
00101 {
00102 QString pluginName=args->getOption("initplugin");
00103
00104 m_initURL=args->url(0);
00105
00106 m_initPlugin= static_cast<Kate::InitPlugin*>(Kate::createPlugin (QFile::encodeName(pluginName), (Kate::Application *)kapp)->qt_cast("Kate::InitPlugin"));
00107
00108 m_initPlugin->activate(args->url(0));
00109
00110 m_doNotInitialize=m_initPlugin->actionsKateShouldNotPerformOnRealStartup();
00111
00112
00113 }
00114
00115
00116 kapp->dcopClient()->resume();
00117
00118
00119 QTimer::singleShot(10,this,SLOT(callOnEventLoopEnter()));
00120 }
00121
00122 KateApp::~KateApp ()
00123 {
00124
00125 delete m_obj;
00126
00127
00128 delete m_pluginManager;
00129
00130
00131 delete m_projectManager;
00132
00133
00134 delete m_docManager;
00135
00136
00137 if (m_sessionConfigDelete)
00138 delete m_sessionConfig;
00139 }
00140
00141 void KateApp::callOnEventLoopEnter()
00142 {
00143 emit onEventLoopEnter();
00144 disconnect(this,SIGNAL(onEventLoopEnter()),0,0);
00145 emit m_application->onEventLoopEnter();
00146 disconnect(m_application,SIGNAL(onEventLoopEnter()),0,0);
00147
00148
00149 }
00150
00151 void KateApp::performInit(const QString &libname, const KURL &url)
00152 {
00153 if (m_initPlugin)
00154 m_oldInitLib=m_initLib;
00155 else
00156 m_oldInitLib=QString::null;
00157
00158 m_initURL=url;
00159 m_initLib=libname;
00160
00161 QTimer::singleShot(0,this,SLOT(performInit()));
00162 }
00163
00164 void KateApp::performInit()
00165 {
00166 if (( m_oldInitLib.isNull()) || (m_oldInitLib!=m_initLib))
00167 {
00168 delete m_initPlugin;
00169 m_initPlugin=0;
00170
00171 if (!m_oldInitLib.isNull())
00172 KLibLoader::self()->unloadLibrary(m_oldInitLib.latin1());
00173
00174 m_initPlugin = static_cast<Kate::InitPlugin*>(Kate::createPlugin (QFile::encodeName(m_initLib), (Kate::Application *)kapp)->qt_cast("Kate::InitPlugin"));
00175 }
00176
00177 m_initPlugin->activate(m_initURL);
00178 m_initPlugin->initKate();
00179 }
00180
00181 Kate::InitPlugin *KateApp::initPlugin() const
00182 {
00183 return m_initPlugin;
00184 }
00185
00186 KURL KateApp::initScript() const {return m_initURL;}
00187
00188 int KateApp::newInstance()
00189 {
00190 if (m_firstStart)
00191 {
00192
00193 if ( restoringSession() )
00194 {
00195
00196 Kate::Document::setOpenErrorDialogsActivated (false);
00197
00198 m_projectManager->restoreProjectList (sessionConfig());
00199 m_docManager->restoreDocumentList (sessionConfig());
00200
00201 Kate::Document::setOpenErrorDialogsActivated (true);
00202
00203 for (int n=1; KMainWindow::canBeRestored(n); n++)
00204 {
00205 KateMainWindow *win=newMainWindow(false);
00206 win->restore ( n, true );
00207 }
00208 }
00209 else
00210 {
00211 Kate::Document::setOpenErrorDialogsActivated (false);
00212 config()->setGroup("General");
00213
00214
00215 if (config()->readBoolEntry("Restore Projects", false))
00216 m_projectManager->restoreProjectList (kateSessionConfig ());
00217
00218
00219 if (config()->readBoolEntry("Restore Documents", false))
00220 m_docManager->restoreDocumentList (kateSessionConfig ());
00221
00222 KateMainWindow *win=newMainWindow(false);
00223
00224
00225 config()->setGroup("General");
00226 if (config()->readBoolEntry("Restore Window Configuration", false))
00227 win->readProperties (kateSessionConfig ());
00228
00229 Kate::Document::setOpenErrorDialogsActivated (true);
00230 win->show ();
00231 }
00232 }
00233
00234
00235 if (mainWindows() == 0)
00236 newMainWindow ();
00237
00238 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00239
00240
00241
00242 KateMainWindow *win = 0;
00243
00244 if (!m_firstStart && args->isSet ("w"))
00245 {
00246 win = newMainWindow ();
00247 }
00248 else
00249 {
00250 for( uint i = 0; i < m_mainWindows.count(); i++ )
00251 {
00252 if( KWin::windowInfo( m_mainWindows.at(i)->winId()).isOnCurrentDesktop() )
00253 {
00254 win = m_mainWindows.at(i);
00255 break;
00256 }
00257 }
00258
00259 if (!win)
00260 {
00261 if (m_mainWindows.count() > 0)
00262
00263 win = m_mainWindows.at(0);
00264 else
00265
00266 win = newMainWindow ();
00267 }
00268 }
00269
00270
00271 if (!m_firstStart)
00272 {
00273 win->raise();
00274 KWin::activateWindow (win->winId());
00275 }
00276
00277 if (m_firstStart && m_initPlugin)
00278 {
00279 m_initPlugin->initKate();
00280 }
00281 else if (args->isSet("initplugin"))
00282 {
00283 performInit(args->getOption("initplugin"),args->url(0));
00284 }
00285 else
00286 {
00287 QTextCodec *codec = args->isSet("encoding") ? QTextCodec::codecForName(args->getOption("encoding")) : 0;
00288
00289 Kate::Document::setOpenErrorDialogsActivated (false);
00290 uint id = 0;
00291 for (int z=0; z<args->count(); z++)
00292 {
00293
00294 bool noDir = !args->url(z).isLocalFile() || !QDir (args->url(z).path()).exists();
00295
00296 if (noDir)
00297 {
00298 if (args->url(z).isLocalFile () && args->url(z).path().endsWith(".kateproject"))
00299 m_mainWindows.first()->openProject ( args->url(z).path() );
00300 else
00301 {
00302
00303
00304 if (codec)
00305 id = m_mainWindows.first()->kateViewManager()->openURL( args->url(z), codec->name(), false );
00306 else
00307 id = m_mainWindows.first()->kateViewManager()->openURL( args->url(z), QString::null, false );
00308 }
00309 }
00310 else
00311 KMessageBox::sorry( m_mainWindows.first(),
00312 i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(args->url(z).url()) );
00313 }
00314
00315 if ( id )
00316 m_mainWindows.first()->kateViewManager()->activateView( id );
00317
00318 Kate::Document::setOpenErrorDialogsActivated (true);
00319
00320 if ( m_mainWindows.first()->kateViewManager()->viewCount () == 0 )
00321 m_mainWindows.first()->kateViewManager()->activateView(m_docManager->firstDocument()->documentNumber());
00322
00323 int line = 0;
00324 int column = 0;
00325 bool nav = false;
00326
00327 if (args->isSet ("line"))
00328 {
00329 line = args->getOption ("line").toInt();
00330 nav = true;
00331 }
00332
00333 if (args->isSet ("column"))
00334 {
00335 column = args->getOption ("column").toInt();
00336 nav = true;
00337 }
00338
00339 if (nav)
00340 m_mainWindows.first()->kateViewManager()->activeView ()->setCursorPosition (line, column);
00341 }
00342
00343 KStartupInfo::setNewStartupId( win, kapp->startupId());
00344
00345 if (m_firstStart)
00346 {
00347
00348 m_firstStart = false;
00349
00350
00351 KTipDialog::showTip(m_mainWindows.first());
00352 }
00353
00354 return 0;
00355 }
00356
00357 KateMainWindow *KateApp::newMainWindow ()
00358 {
00359 return newMainWindow (true);
00360 }
00361
00362 KateMainWindow *KateApp::newMainWindow (bool visible)
00363 {
00364 KateMainWindow *mainWindow = new KateMainWindow ();
00365 m_mainWindows.append (mainWindow);
00366
00367 if ((mainWindows() > 1) && m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView())
00368 mainWindow->kateViewManager()->activateView ( m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView()->getDoc()->documentNumber() );
00369 else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
00370 mainWindow->kateViewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
00371 else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
00372 mainWindow->kateViewManager()->openURL ( KURL() );
00373
00374 if (visible)
00375 mainWindow->show ();
00376
00377
00378 if (!m_firstStart)
00379 {
00380 mainWindow->raise();
00381 KWin::activateWindow (mainWindow->winId());
00382 }
00383
00384 return mainWindow;
00385 }
00386
00387 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
00388 {
00389 m_mainWindows.remove (mainWindow);
00390 }
00391
00392 void KateApp::openURL (const QString &name)
00393 {
00394 int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00395
00396 if (n < 0)
00397 n=0;
00398
00399 m_mainWindows.at(n)->kateViewManager()->openURL (KURL(name));
00400
00401 if ( ! m_firstStart )
00402 {
00403 m_mainWindows.at(n)->raise();
00404 KWin::activateWindow (m_mainWindows.at(n)->winId());
00405 }
00406 }
00407
00408 KateMainWindow *KateApp::activeKateMainWindow ()
00409 {
00410 if (m_mainWindows.isEmpty())
00411 return 0;
00412
00413 int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00414
00415 if (n < 0)
00416 n=0;
00417
00418 return m_mainWindows.at(n);
00419 }
00420
00421 Kate::MainWindow *KateApp::activeMainWindow ()
00422 {
00423 if (!activeKateMainWindow())
00424 return 0;
00425
00426 return activeKateMainWindow()->mainWindow();
00427 }
00428
00429
This file is part of the documentation for kate Library Version 3.4.2.