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