kate Library API Documentation

kwritemain.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
00003    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00004    Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "kwritemain.h"
00022 #include "kwritemain.moc"
00023 
00024 #include <kate/document.h>
00025 #include <kate/view.h>
00026 
00027 #include <ktexteditor/configinterface.h>
00028 #include <ktexteditor/sessionconfiginterface.h>
00029 #include <ktexteditor/viewcursorinterface.h>
00030 #include <ktexteditor/printinterface.h>
00031 #include <ktexteditor/encodinginterface.h>
00032 #include <ktexteditor/editorchooser.h>
00033 #include <ktexteditor/popupmenuinterface.h>
00034 
00035 #include <kio/netaccess.h>
00036 
00037 #include <dcopclient.h>
00038 #include <kurldrag.h>
00039 #include <kencodingfiledialog.h>
00040 #include <kdiroperator.h>
00041 #include <kiconloader.h>
00042 #include <kaboutdata.h>
00043 #include <kstatusbar.h>
00044 #include <kstdaction.h>
00045 #include <kaction.h>
00046 #include <kdebug.h>
00047 #include <kglobal.h>
00048 #include <kapplication.h>
00049 #include <klocale.h>
00050 #include <kurl.h>
00051 #include <kconfig.h>
00052 #include <kcmdlineargs.h>
00053 #include <kmessagebox.h>
00054 #include <kkeydialog.h>
00055 #include <kedittoolbar.h>
00056 #include <kparts/event.h>
00057 #include <kmenubar.h>
00058 
00059 #include <qdropsite.h>
00060 #include <qdragobject.h>
00061 #include <qvbox.h>
00062 #include <qtextcodec.h>
00063 #include <qlayout.h>
00064 
00065 // StatusBar field IDs
00066 #define KWRITE_ID_GEN 1
00067 
00068 QPtrList<KTextEditor::Document> KWrite::docList;
00069 QPtrList<KWrite> KWrite::winList;
00070 
00071 KWrite::KWrite (KTextEditor::Document *doc)
00072     : m_view(0),
00073       m_recentFiles(0),
00074       m_paShowPath(0),
00075       m_paShowStatusBar(0)
00076 {
00077   setMinimumSize(200,200);
00078 
00079   if ( !doc )
00080   {
00081     if ( !(doc = KTextEditor::EditorChooser::createDocument(0,"KTextEditor::Document")) )
00082     {
00083       KMessageBox::error(this, i18n("A KDE text-editor component could not be found;\n"
00084                                     "please check your KDE installation."));
00085       kapp->exit(1);
00086     }
00087 
00088     docList.append(doc);
00089   }
00090 
00091   m_view = doc->createView (this, 0L);
00092 
00093   setCentralWidget(m_view);
00094 
00095   setupActions();
00096   setupStatusBar();
00097 
00098   setAcceptDrops(true);
00099 
00100   connect(m_view,SIGNAL(newStatus()),this,SLOT(newCaption()));
00101   connect(m_view,SIGNAL(viewStatusMsg(const QString &)),this,SLOT(newStatus(const QString &)));
00102   connect(m_view->document(),SIGNAL(fileNameChanged()),this,SLOT(newCaption()));
00103   connect(m_view->document(),SIGNAL(fileNameChanged()),this,SLOT(slotFileNameChanged()));
00104   connect(m_view,SIGNAL(dropEventPass(QDropEvent *)),this,SLOT(slotDropEvent(QDropEvent *)));
00105 
00106   setXMLFile( "kwriteui.rc" );
00107   createShellGUI( true );
00108   guiFactory()->addClient( m_view );
00109 
00110   // install a working kate part popup dialog thingy
00111   if (static_cast<Kate::View*>(m_view->qt_cast("Kate::View")))
00112     static_cast<Kate::View*>(m_view->qt_cast("Kate::View"))->installPopup ((QPopupMenu*)(factory()->container("ktexteditor_popup", this)) );
00113 
00114   // call it as last thing, must be sure everything is already set up ;)
00115   setAutoSaveSettings ("MainWindow Settings");
00116 
00117   // init with more usefull size, stolen from konq :)
00118   if ( !initialGeometrySet() && !kapp->config()->hasGroup("MainWindow Settings"))
00119     resize( 700, 480 );
00120 
00121   readConfig ();
00122 
00123   winList.append (this);
00124 
00125   show ();
00126 }
00127 
00128 KWrite::~KWrite()
00129 {
00130   winList.remove (this);
00131 
00132   if (m_view->document()->views().count() == 1)
00133   {
00134     docList.remove(m_view->document());
00135     delete m_view->document();
00136   }
00137 
00138   kapp->config()->sync ();
00139 }
00140 
00141 void KWrite::setupActions()
00142 {
00143   KStdAction::close( this, SLOT(slotFlush()), actionCollection(), "file_close" )->setWhatsThis(i18n("Use this to close the current document"));
00144 
00145   // setup File menu
00146   KStdAction::print(this, SLOT(printDlg()), actionCollection())->setWhatsThis(i18n("Use this command to print the current document"));
00147   KStdAction::openNew( this, SLOT(slotNew()), actionCollection(), "file_new" )->setWhatsThis(i18n("Use this command to create a new document"));
00148   KStdAction::open( this, SLOT( slotOpen() ), actionCollection(), "file_open" )->setWhatsThis(i18n("Use this command to open an existing document for editing"));
00149 
00150   m_recentFiles = KStdAction::openRecent(this, SLOT(slotOpen(const KURL&)),
00151                                          actionCollection());
00152   m_recentFiles->setWhatsThis(i18n("This lists files which you have opened recently, and allows you to easily open them again."));
00153 
00154   KAction *a=new KAction(i18n("&New Window"), "window_new", 0, this, SLOT(newView()),
00155               actionCollection(), "view_new_view");
00156   a->setWhatsThis(i18n("Create another view containing the current document"));
00157 
00158   a=new KAction(i18n("Choose Editor..."),0,this,SLOT(changeEditor()),
00159         actionCollection(),"settings_choose_editor");
00160   a->setWhatsThis(i18n("Override the system wide setting for the default editing component"));
00161 
00162   KStdAction::quit(this, SLOT(close()), actionCollection())->setWhatsThis(i18n("Close the current document view"));
00163 
00164   // setup Settings menu
00165   setStandardToolBarMenuEnabled(true);
00166 
00167   m_paShowStatusBar = KStdAction::showStatusbar(this, SLOT(toggleStatusBar()), actionCollection(), "settings_show_statusbar");
00168   m_paShowStatusBar->setWhatsThis(i18n("Use this command to show or hide the view's statusbar"));
00169 
00170   m_paShowPath = new KToggleAction(i18n("Sho&w Path"), 0, this, SLOT(newCaption()),
00171                     actionCollection(), "set_showPath");
00172   m_paShowPath->setCheckedState(i18n("Hide Path"));
00173   m_paShowPath->setWhatsThis(i18n("Show the complete document path in the window caption"));
00174   a=KStdAction::keyBindings(this, SLOT(editKeys()), actionCollection());
00175   a->setWhatsThis(i18n("Configure the application's keyboard shortcut assignments."));
00176 
00177   a=KStdAction::configureToolbars(this, SLOT(editToolbars()), actionCollection(), "set_configure_toolbars");
00178   a->setWhatsThis(i18n("Configure which items should appear in the toolbar(s)."));
00179 }
00180 
00181 void KWrite::setupStatusBar()
00182 {
00183   statusBar()->insertItem("", KWRITE_ID_GEN);
00184 }
00185 
00186 // load on url
00187 void KWrite::loadURL(const KURL &url)
00188 {
00189   m_view->document()->openURL(url);
00190 }
00191 
00192 // is closing the window wanted by user ?
00193 bool KWrite::queryClose()
00194 {
00195   if (m_view->document()->views().count() > 1)
00196     return true;
00197 
00198   if (m_view->document()->queryClose())
00199   {
00200     writeConfig();
00201 
00202     return true;
00203   }
00204 
00205   return false;
00206 }
00207 
00208 void KWrite::changeEditor()
00209 {
00210   KWriteEditorChooser choose(this);
00211   choose.exec();
00212 }
00213 
00214 void KWrite::slotFlush ()
00215 {
00216    m_view->document()->closeURL();
00217 }
00218 
00219 void KWrite::slotNew()
00220 {
00221   if (m_view->document()->isModified() || !m_view->document()->url().isEmpty())
00222     new KWrite();
00223   else
00224     m_view->document()->openURL(KURL());
00225 }
00226 
00227 void KWrite::slotOpen()
00228 {
00229   if (KTextEditor::encodingInterface(m_view->document()))
00230   {
00231     KEncodingFileDialog::Result r=KEncodingFileDialog::getOpenURLsAndEncoding(
00232         KTextEditor::encodingInterface(m_view->document())->encoding(),
00233     m_view->document()->url().url(),QString::null,this,i18n("Open File"));
00234 
00235     for (KURL::List::Iterator i=r.URLs.begin(); i != r.URLs.end(); ++i)
00236     {
00237       encoding = r.encoding;
00238       slotOpen ( *i );
00239     }
00240   }
00241   else
00242   {
00243     KURL::List l=KFileDialog::getOpenURLs(m_view->document()->url().url(),QString::null,this,QString::null);
00244     for (KURL::List::Iterator i=l.begin(); i != l.end(); ++i)
00245     {
00246       slotOpen ( *i );
00247     }
00248   }
00249 }
00250 
00251 void KWrite::slotOpen( const KURL& url )
00252 {
00253   if (url.isEmpty()) return;
00254 
00255   if (!KIO::NetAccess::exists(url, true, this))
00256   {
00257     KMessageBox::error (this, i18n("The given file could not be read, check if it exists or if it is readable for the current user."));
00258     return;
00259   }
00260 
00261   if (m_view->document()->isModified() || !m_view->document()->url().isEmpty())
00262   {
00263     KWrite *t = new KWrite();
00264     if (KTextEditor::encodingInterface(t->m_view->document())) KTextEditor::encodingInterface(t->m_view->document())->setEncoding(encoding);
00265     t->loadURL(url);
00266   }
00267   else
00268   {
00269     if (KTextEditor::encodingInterface(m_view->document())) KTextEditor::encodingInterface(m_view->document())->setEncoding(encoding);
00270     loadURL(url);
00271   }
00272 }
00273 
00274 void KWrite::slotFileNameChanged()
00275 {
00276   if ( ! m_view->document()->url().isEmpty() )
00277     m_recentFiles->addURL( m_view->document()->url() );
00278 }
00279 
00280 void KWrite::newView()
00281 {
00282   new KWrite(m_view->document());
00283 }
00284 
00285 void KWrite::toggleStatusBar()
00286 {
00287   if( m_paShowStatusBar->isChecked() )
00288     statusBar()->show();
00289   else
00290     statusBar()->hide();
00291 }
00292 
00293 void KWrite::editKeys()
00294 {
00295   KKeyDialog dlg;
00296   dlg.insert(actionCollection());
00297   if( m_view )
00298     dlg.insert(m_view->actionCollection());
00299   dlg.configure();
00300 }
00301 
00302 void KWrite::editToolbars()
00303 {
00304   KEditToolbar *dlg = new KEditToolbar(guiFactory());
00305 
00306   if (dlg->exec())
00307   {
00308     KParts::GUIActivateEvent ev1( false );
00309     QApplication::sendEvent( m_view, &ev1 );
00310     guiFactory()->removeClient( m_view );
00311     createShellGUI( false );
00312     createShellGUI( true );
00313     guiFactory()->addClient( m_view );
00314     KParts::GUIActivateEvent ev2( true );
00315     QApplication::sendEvent( m_view, &ev2 );
00316   }
00317 
00318   delete dlg;
00319 }
00320 
00321 void KWrite::printNow()
00322 {
00323   KTextEditor::printInterface(m_view->document())->print ();
00324 }
00325 
00326 void KWrite::printDlg()
00327 {
00328   KTextEditor::printInterface(m_view->document())->printDialog ();
00329 }
00330 
00331 void KWrite::newStatus(const QString &msg)
00332 {
00333   newCaption();
00334 
00335   statusBar()->changeItem(msg,KWRITE_ID_GEN);
00336 }
00337 
00338 void KWrite::newCaption()
00339 {
00340   if (m_view->document()->url().isEmpty()) {
00341     setCaption(i18n("Untitled"),m_view->document()->isModified());
00342   }
00343   else
00344   {
00345     QString c;
00346     if (!m_paShowPath->isChecked())
00347     {
00348       c = m_view->document()->url().fileName();
00349 
00350       //File name shouldn't be too long - Maciek
00351       if (c.length() > 64)
00352         c = c.left(64) + "...";
00353     }
00354     else
00355     {
00356       c = m_view->document()->url().prettyURL();
00357 
00358       //File name shouldn't be too long - Maciek
00359       if (c.length() > 64)
00360         c = "..." + c.right(64);
00361     }
00362 
00363     setCaption (c, m_view->document()->isModified());
00364   }
00365 }
00366 
00367 void KWrite::dragEnterEvent( QDragEnterEvent *event )
00368 {
00369   event->accept(KURLDrag::canDecode(event));
00370 }
00371 
00372 void KWrite::dropEvent( QDropEvent *event )
00373 {
00374   slotDropEvent(event);
00375 }
00376 
00377 void KWrite::slotDropEvent( QDropEvent *event )
00378 {
00379   KURL::List textlist;
00380 
00381   if (!KURLDrag::decode(event, textlist))
00382     return;
00383 
00384   for (KURL::List::Iterator i=textlist.begin(); i != textlist.end(); ++i)
00385     slotOpen (*i);
00386 }
00387 
00388 void KWrite::slotEnableActions( bool enable )
00389 {
00390   QValueList<KAction *> actions = actionCollection()->actions();
00391   QValueList<KAction *>::ConstIterator it = actions.begin();
00392   QValueList<KAction *>::ConstIterator end = actions.end();
00393 
00394   for (; it != end; ++it )
00395       (*it)->setEnabled( enable );
00396 
00397   actions = m_view->actionCollection()->actions();
00398   it = actions.begin();
00399   end = actions.end();
00400 
00401   for (; it != end; ++it )
00402       (*it)->setEnabled( enable );
00403 }
00404 
00405 //common config
00406 void KWrite::readConfig(KConfig *config)
00407 {
00408   config->setGroup("General Options");
00409 
00410   m_paShowStatusBar->setChecked( config->readBoolEntry("ShowStatusBar") );
00411   m_paShowPath->setChecked( config->readBoolEntry("ShowPath") );
00412 
00413   m_recentFiles->loadEntries(config, "Recent Files");
00414 
00415   if (m_view && KTextEditor::configInterface(m_view->document()))
00416     KTextEditor::configInterface(m_view->document())->readConfig(config);
00417 
00418   if( m_paShowStatusBar->isChecked() )
00419     statusBar()->show();
00420   else
00421     statusBar()->hide();
00422 }
00423 
00424 void KWrite::writeConfig(KConfig *config)
00425 {
00426   config->setGroup("General Options");
00427 
00428   config->writeEntry("ShowStatusBar",m_paShowStatusBar->isChecked());
00429   config->writeEntry("ShowPath",m_paShowPath->isChecked());
00430 
00431   m_recentFiles->saveEntries(config, "Recent Files");
00432 
00433   if (m_view && KTextEditor::configInterface(m_view->document()))
00434     KTextEditor::configInterface(m_view->document())->writeConfig(config);
00435 
00436   config->sync ();
00437 }
00438 
00439 //config file
00440 void KWrite::readConfig()
00441 {
00442   KConfig *config = kapp->config();
00443   readConfig(config);
00444 }
00445 
00446 void KWrite::writeConfig()
00447 {
00448   KConfig *config = kapp->config();
00449   writeConfig(config);
00450 }
00451 
00452 // session management
00453 void KWrite::restore(KConfig *config, int n)
00454 {
00455   readPropertiesInternal(config, n);
00456 }
00457 
00458 void KWrite::readProperties(KConfig *config)
00459 {
00460   readConfig(config);
00461 
00462   if (KTextEditor::sessionConfigInterface(m_view))
00463     KTextEditor::sessionConfigInterface(m_view)->readSessionConfig(config);
00464 }
00465 
00466 void KWrite::saveProperties(KConfig *config)
00467 {
00468   writeConfig(config);
00469   config->writeEntry("DocumentNumber",docList.find(m_view->document()) + 1);
00470 
00471   if (KTextEditor::sessionConfigInterface(m_view))
00472     KTextEditor::sessionConfigInterface(m_view)->writeSessionConfig(config);
00473 }
00474 
00475 void KWrite::saveGlobalProperties(KConfig *config) //save documents
00476 {
00477   config->setGroup("Number");
00478   config->writeEntry("NumberOfDocuments",docList.count());
00479 
00480   for (uint z = 1; z <= docList.count(); z++)
00481   {
00482      QString buf = QString("Document %1").arg(z);
00483      config->setGroup(buf);
00484 
00485      KTextEditor::Document *doc = docList.at(z - 1);
00486 
00487      if (KTextEditor::configInterface(doc))
00488        KTextEditor::configInterface(doc)->writeSessionConfig(config);
00489   }
00490 
00491   for (uint z = 1; z <= winList.count(); z++)
00492   {
00493      QString buf = QString("Window %1").arg(z);
00494      config->setGroup(buf);
00495 
00496      config->writeEntry("DocumentNumber",docList.find(winList.at(z-1)->view()->document()) + 1);
00497   }
00498 }
00499 
00500 //restore session
00501 void KWrite::restore()
00502 {
00503   KConfig *config = kapp->sessionConfig();
00504 
00505   if (!config)
00506     return;
00507 
00508   int docs, windows;
00509   QString buf;
00510   KTextEditor::Document *doc;
00511   KWrite *t;
00512 
00513   config->setGroup("Number");
00514   docs = config->readNumEntry("NumberOfDocuments");
00515   windows = config->readNumEntry("NumberOfWindows");
00516 
00517   for (int z = 1; z <= docs; z++)
00518   {
00519      buf = QString("Document %1").arg(z);
00520      config->setGroup(buf);
00521      doc=KTextEditor::EditorChooser::createDocument(0,"KTextEditor::Document");
00522 
00523      if (KTextEditor::configInterface(doc))
00524        KTextEditor::configInterface(doc)->readSessionConfig(config);
00525      docList.append(doc);
00526   }
00527 
00528   for (int z = 1; z <= windows; z++)
00529   {
00530     buf = QString("Window %1").arg(z);
00531     config->setGroup(buf);
00532     t = new KWrite(docList.at(config->readNumEntry("DocumentNumber") - 1));
00533     t->restore(config,z);
00534   }
00535 }
00536 
00537 static KCmdLineOptions options[] =
00538 {
00539   { "stdin",    I18N_NOOP("Read the contents of stdin"), 0},
00540   { "encoding <argument>",      I18N_NOOP("Set encoding for the file to open"), 0 },
00541   { "line <argument>",      I18N_NOOP("Navigate to this line"), 0 },
00542   { "column <argument>",      I18N_NOOP("Navigate to this column"), 0 },
00543   { "+[URL]",   I18N_NOOP("Document to open"), 0 },
00544   KCmdLineLastOption
00545 };
00546 
00547 extern "C" KDE_EXPORT int kdemain(int argc, char **argv)
00548 {
00549   Kate::Document::setFileChangedDialogsActivated (true);
00550 
00551   KLocale::setMainCatalogue("kate");         //lukas: set this to have the kwritepart translated using kate message catalog
00552 
00553   KAboutData aboutData ( "kwrite", I18N_NOOP("KWrite"), "4.4",
00554                          I18N_NOOP( "KWrite - Text Editor" ), KAboutData::License_LGPL_V2,
00555                          I18N_NOOP( "(c) 2000-2004 The Kate Authors" ), 0, "http://kate.kde.org" );
00556 
00557   aboutData.addAuthor ("Christoph Cullmann", I18N_NOOP("Maintainer"), "cullmann@kde.org", "http://www.babylon2k.de");
00558   aboutData.addAuthor ("Anders Lund", I18N_NOOP("Core Developer"), "anders@alweb.dk", "http://www.alweb.dk");
00559   aboutData.addAuthor ("Joseph Wenninger", I18N_NOOP("Core Developer"), "jowenn@kde.org","http://stud3.tuwien.ac.at/~e9925371");
00560   aboutData.addAuthor ("Hamish Rodda",I18N_NOOP("Core Developer"), "rodda@kde.org");
00561   aboutData.addAuthor ("Waldo Bastian", I18N_NOOP( "The cool buffersystem" ), "bastian@kde.org" );
00562   aboutData.addAuthor ("Charles Samuels", I18N_NOOP("The Editing Commands"), "charles@kde.org");
00563   aboutData.addAuthor ("Matt Newell", I18N_NOOP("Testing, ..."), "newellm@proaxis.com");
00564   aboutData.addAuthor ("Michael Bartl", I18N_NOOP("Former Core Developer"), "michael.bartl1@chello.at");
00565   aboutData.addAuthor ("Michael McCallum", I18N_NOOP("Core Developer"), "gholam@xtra.co.nz");
00566   aboutData.addAuthor ("Jochen Wilhemly", I18N_NOOP( "KWrite Author" ), "digisnap@cs.tu-berlin.de" );
00567   aboutData.addAuthor ("Michael Koch",I18N_NOOP("KWrite port to KParts"), "koch@kde.org");
00568   aboutData.addAuthor ("Christian Gebauer", 0, "gebauer@kde.org" );
00569   aboutData.addAuthor ("Simon Hausmann", 0, "hausmann@kde.org" );
00570   aboutData.addAuthor ("Glen Parker",I18N_NOOP("KWrite Undo History, Kspell integration"), "glenebob@nwlink.com");
00571   aboutData.addAuthor ("Scott Manson",I18N_NOOP("KWrite XML Syntax highlighting support"), "sdmanson@alltel.net");
00572   aboutData.addAuthor ("John Firebaugh",I18N_NOOP("Patches and more"), "jfirebaugh@kde.org");
00573 
00574   aboutData.addCredit ("Matteo Merli",I18N_NOOP("Highlighting for RPM Spec-Files, Perl, Diff and more"), "merlim@libero.it");
00575   aboutData.addCredit ("Rocky Scaletta",I18N_NOOP("Highlighting for VHDL"), "rocky@purdue.edu");
00576   aboutData.addCredit ("Yury Lebedev",I18N_NOOP("Highlighting for SQL"),"");
00577   aboutData.addCredit ("Chris Ross",I18N_NOOP("Highlighting for Ferite"),"");
00578   aboutData.addCredit ("Nick Roux",I18N_NOOP("Highlighting for ILERPG"),"");
00579   aboutData.addCredit ("Carsten Niehaus", I18N_NOOP("Highlighting for LaTeX"),"");
00580   aboutData.addCredit ("Per Wigren", I18N_NOOP("Highlighting for Makefiles, Python"),"");
00581   aboutData.addCredit ("Jan Fritz", I18N_NOOP("Highlighting for Python"),"");
00582   aboutData.addCredit ("Daniel Naber","","");
00583   aboutData.addCredit ("Roland Pabel",I18N_NOOP("Highlighting for Scheme"),"");
00584   aboutData.addCredit ("Cristi Dumitrescu",I18N_NOOP("PHP Keyword/Datatype list"),"");
00585   aboutData.addCredit ("Carsten Pfeiffer", I18N_NOOP("Very nice help"), "");
00586   aboutData.addCredit (I18N_NOOP("All people who have contributed and I have forgotten to mention"),"","");
00587 
00588   aboutData.setTranslator(I18N_NOOP("_: NAME OF TRANSLATORS\nYour names"), I18N_NOOP("_: EMAIL OF TRANSLATORS\nYour emails"));
00589 
00590   KCmdLineArgs::init( argc, argv, &aboutData );
00591   KCmdLineArgs::addCmdLineOptions( options );
00592 
00593   KApplication a;
00594 
00595   KGlobal::locale()->insertCatalogue("katepart");
00596 
00597   DCOPClient *client = kapp->dcopClient();
00598   if (!client->isRegistered())
00599   {
00600     client->attach();
00601     client->registerAs("kwrite");
00602   }
00603 
00604   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00605 
00606   if (kapp->isRestored())
00607   {
00608     KWrite::restore();
00609   }
00610   else
00611   {
00612     bool nav = false;
00613     int line = 0, column = 0;
00614 
00615     QTextCodec *codec = args->isSet("encoding") ? QTextCodec::codecForName(args->getOption("encoding")) : 0;
00616 
00617     if (args->isSet ("line"))
00618     {
00619       line = args->getOption ("line").toInt();
00620       nav = true;
00621     }
00622 
00623     if (args->isSet ("column"))
00624     {
00625       column = args->getOption ("column").toInt();
00626       nav = true;
00627     }
00628 
00629     if ( args->count() == 0 )
00630     {
00631         KWrite *t = new KWrite;
00632 
00633         if( args->isSet( "stdin" ) )
00634         {
00635           QTextIStream input(stdin);
00636 
00637           // set chosen codec
00638           if (codec)
00639             input.setCodec (codec);
00640 
00641           QString line;
00642           QString text;
00643 
00644           do
00645           {
00646             line = input.readLine();
00647             text.append( line + "\n" );
00648           } while( !line.isNull() );
00649 
00650 
00651           KTextEditor::EditInterface *doc = KTextEditor::editInterface (t->view()->document());
00652           if( doc )
00653               doc->setText( text );
00654         }
00655 
00656         if (nav && KTextEditor::viewCursorInterface(t->view()))
00657           KTextEditor::viewCursorInterface(t->view())->setCursorPosition (line, column);
00658     }
00659     else
00660     {
00661       for ( int z = 0; z < args->count(); z++ )
00662       {
00663         KWrite *t = new KWrite();
00664 
00665         // this file is no local dir, open it, else warn
00666         bool noDir = !args->url(z).isLocalFile() || !QDir (args->url(z).path()).exists();
00667 
00668         if (noDir)
00669         {
00670           if (Kate::document (t->view()->document()))
00671             Kate::Document::setOpenErrorDialogsActivated (false);
00672 
00673           if (codec && KTextEditor::encodingInterface(t->view()->document()))
00674             KTextEditor::encodingInterface(t->view()->document())->setEncoding(codec->name());
00675 
00676           t->loadURL( args->url( z ) );
00677 
00678           if (Kate::document (t->view()->document()))
00679             Kate::Document::setOpenErrorDialogsActivated (true);
00680 
00681           if (nav && KTextEditor::viewCursorInterface(t->view()))
00682             KTextEditor::viewCursorInterface(t->view())->setCursorPosition (line, column);
00683         }
00684         else
00685           KMessageBox::sorry( t, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(args->url(z).url()) );
00686       }
00687     }
00688   }
00689 
00690   // no window there, uh, ohh, for example borked session config !!!
00691   // create at least one !!
00692   if (KWrite::noWindows())
00693     new KWrite();
00694 
00695   return a.exec ();
00696 }
00697 
00698 KWriteEditorChooser::KWriteEditorChooser(QWidget *):
00699     KDialogBase(KDialogBase::Plain,i18n("Choose Editor Component"),KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Cancel)
00700 {
00701     (new QVBoxLayout(plainPage()))->setAutoAdd(true);
00702     m_chooser=new KTextEditor::EditorChooser(plainPage(),"Editor Chooser");
00703     setMainWidget(m_chooser);
00704     m_chooser->readAppSetting();
00705 }
00706 
00707 KWriteEditorChooser::~KWriteEditorChooser() {
00708 ;
00709 }
00710 
00711 void KWriteEditorChooser::slotOk() {
00712     m_chooser->writeAppSetting();
00713     KDialogBase::slotOk();
00714 }
KDE Logo
This file is part of the documentation for kate Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Sep 30 18:40:32 2005 by doxygen 1.4.3 written by Dimitri van Heesch, © 1997-2003