ksync Library API Documentation

ksync.cpp

00001 #include <qdir.h>
00002 #include <qprinter.h>
00003 #include <qpainter.h>
00004 
00005 #include <kiconloader.h>
00006 #include <kmessagebox.h>
00007 #include <kfiledialog.h>
00008 #include <kmenubar.h>
00009 #include <klocale.h>
00010 #include <kconfig.h>
00011 #include <kstdaction.h>
00012 
00013 #include "ksync.h"
00014 #include "ksync.moc"
00015 #include "ksyncview.h"
00016 #include <kstatusbar.h>
00017 
00018 #define ID_STATUS_MSG 1
00019 
00020 KSync::KSync(QWidget* , const char* name):KMainWindow(0, name)
00021 {
00022   config=kapp->config();
00023 
00024   initStatusBar();
00025   initActions();
00026   initView();
00027         
00028   readOptions();
00029 
00030   // disable actions at startup
00031   fileSave->setEnabled(false);
00032   fileSaveAs->setEnabled(false);
00033   filePrint->setEnabled(false);
00034   editCut->setEnabled(false);
00035   editCopy->setEnabled(false);
00036   editPaste->setEnabled(false);
00037 }
00038 
00039 KSync::~KSync()
00040 {
00041 
00042 }
00043 
00044 void KSync::initActions()
00045 {
00046   fileNewWindow = new KAction(i18n("New &Window"), 0, 0, this, SLOT(slotFileNewWindow()), actionCollection(),"file_new_window");
00047   fileNew = KStdAction::openNew(this, SLOT(slotFileNew()), actionCollection());
00048   fileOpen = KStdAction::open(this, SLOT(slotFileOpen()), actionCollection());
00049   fileOpenRecent = KStdAction::openRecent(this, SLOT(slotFileOpenRecent(const KURL&)), actionCollection());
00050   fileSave = KStdAction::save(this, SLOT(slotFileSave()), actionCollection());
00051   fileSaveAs = KStdAction::saveAs(this, SLOT(slotFileSaveAs()), actionCollection());
00052   fileClose = KStdAction::close(this, SLOT(slotFileClose()), actionCollection());
00053   filePrint = KStdAction::print(this, SLOT(slotFilePrint()), actionCollection());
00054   fileQuit = KStdAction::quit(this, SLOT(slotFileQuit()), actionCollection());
00055   editCut = KStdAction::cut(this, SLOT(slotEditCut()), actionCollection());
00056   editCopy = KStdAction::copy(this, SLOT(slotEditCopy()), actionCollection());
00057   editPaste = KStdAction::paste(this, SLOT(slotEditPaste()), actionCollection());
00058   viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
00059   viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
00060 
00061   fileNewWindow->setStatusText(i18n("Opens a new application window"));
00062   fileNew->setStatusText(i18n("Creates a new document"));
00063   fileOpen->setStatusText(i18n("Opens an existing document"));
00064   fileOpenRecent->setStatusText(i18n("Opens a recently used file"));
00065   fileSave->setStatusText(i18n("Saves the actual document"));
00066   fileSaveAs->setStatusText(i18n("Saves the actual document as..."));
00067   fileClose->setStatusText(i18n("Closes the actual document"));
00068   filePrint ->setStatusText(i18n("Prints out the actual document"));
00069   fileQuit->setStatusText(i18n("Quits the application"));
00070   editCut->setStatusText(i18n("Cuts the selected section and puts it to the clipboard"));
00071   editCopy->setStatusText(i18n("Copies the selected section to the clipboard"));
00072   editPaste->setStatusText(i18n("Pastes the clipboard contents to actual position"));
00073   viewToolBar->setStatusText(i18n("Enables/disables the toolbar"));
00074   viewStatusBar->setStatusText(i18n("Enables/disables the statusbar"));
00075 
00076   // use the absolute path to your ksyncui.rc file for testing purpose in createGUI();
00077   createGUI();
00078 
00079 }
00080 
00081 
00082 void KSync::initStatusBar()
00083 {
00084   statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
00085 }
00086 
00087 void KSync::initView()
00088 {
00089   mView = new KSyncView(this);
00090   setCentralWidget(mView);      
00091 //  setCaption(doc->URL().fileName(),false);
00092 }
00093 
00094 void KSync::openDocumentFile(const KURL& url)
00095 {
00096   slotStatusMsg(i18n("Opening file..."));
00097 
00098 //  doc->openDocument( url);
00099   fileOpenRecent->addURL( url );
00100   slotStatusMsg(i18n("Ready."));
00101 }
00102 
00103 
00104 void KSync::saveOptions()
00105 {       
00106   config->setGroup("General Options");
00107   config->writeEntry("Geometry", size());
00108   config->writeEntry("Show Toolbar", viewToolBar->isChecked());
00109   config->writeEntry("Show Statusbar",viewStatusBar->isChecked());
00110   config->writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
00111   fileOpenRecent->saveEntries(config,"Recent Files");
00112 
00113   mView->writeConfig(config);
00114 }
00115 
00116 
00117 void KSync::readOptions()
00118 {
00119   config->setGroup("General Options");
00120 
00121   // bar status settings
00122   bool bViewToolbar = config->readBoolEntry("Show Toolbar", true);
00123   viewToolBar->setChecked(bViewToolbar);
00124   slotViewToolBar();
00125 
00126   bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true);
00127   viewStatusBar->setChecked(bViewStatusbar);
00128   slotViewStatusBar();
00129 
00130 
00131   // bar position settings
00132   KToolBar::BarPosition toolBarPos;
00133   toolBarPos=(KToolBar::BarPosition) config->readNumEntry("ToolBarPos", KToolBar::Top);
00134   toolBar("mainToolBar")->setBarPos(toolBarPos);
00135         
00136   // initialize the recent file list
00137   fileOpenRecent->loadEntries(config,"Recent Files");
00138 
00139   QSize size=config->readSizeEntry("Geometry");
00140   if(!size.isEmpty())
00141   {
00142     resize(size);
00143   }
00144 
00145   mView->readConfig(config);
00146 }
00147 
00148 void KSync::saveProperties(KConfig *)
00149 {
00150 #if 0
00151   if(doc->URL().fileName()!=i18n("Untitled") && !doc->isModified())
00152   {
00153     // saving to tempfile not necessary
00154 
00155   }
00156   else
00157   {
00158     KURL url=doc->URL();        
00159     _cfg->writeEntry("filename", url.url());
00160     _cfg->writeEntry("modified", doc->isModified());
00161     QString tempname = kapp->tempSaveName(url.url());
00162     QString tempurl= KURL::encode_string(tempname);
00163     KURL _url(tempurl);
00164     doc->saveDocument(_url);
00165   }
00166 #endif
00167 }
00168 
00169 
00170 void KSync::readProperties(KConfig *)
00171 {
00172 #if 0
00173   QString filename = _cfg->readEntry("filename", "");
00174   KURL url(filename);
00175   bool modified = _cfg->readBoolEntry("modified", false);
00176   if(modified)
00177   {
00178     bool canRecover;
00179     QString tempname = kapp->checkRecoverFile(filename, canRecover);
00180     KURL _url(tempname);
00181         
00182     if(canRecover)
00183     {
00184       doc->openDocument(_url);
00185       doc->setModified();
00186       setCaption(_url.fileName(),true);
00187       QFile::remove(tempname);
00188     }
00189   }
00190   else
00191   {
00192     if(!filename.isEmpty())
00193     {
00194       doc->openDocument(url);
00195       setCaption(url.fileName(),false);
00196     }
00197   }
00198 #endif
00199 }
00200 
00201 bool KSync::queryClose()
00202 {
00203 //  return doc->saveModified();
00204   return true;
00205 }
00206 
00207 bool KSync::queryExit()
00208 {
00209   saveOptions();
00210   return true;
00211 }
00212 
00213 void KSync::slotFileNewWindow()
00214 {
00215   slotStatusMsg(i18n("Opening a new application window..."));
00216         
00217   KSync *new_window= new KSync();
00218   new_window->show();
00219 
00220   slotStatusMsg(i18n("Ready."));
00221 }
00222 
00223 void KSync::slotFileNew()
00224 {
00225   slotStatusMsg(i18n("Creating new document..."));
00226 
00227 #if 0
00228   if(!doc->saveModified())
00229   {
00230      // here saving wasn't successful
00231 
00232   }
00233   else
00234   {     
00235     doc->newDocument();         
00236     setCaption(doc->URL().fileName(), false);
00237   }
00238 #endif
00239 
00240   slotStatusMsg(i18n("Ready."));
00241 }
00242 
00243 void KSync::slotFileOpen()
00244 {
00245   slotStatusMsg(i18n("Opening file..."));
00246 
00247 #if 0   
00248   if(!doc->saveModified())
00249   {
00250      // here saving wasn't successful
00251 
00252   }
00253   else
00254   {     
00255     KURL url=KFileDialog::getOpenURL(QString::null,
00256         i18n("*|All files"), this, i18n("Open File"));
00257     if(!url.isEmpty())
00258     {
00259       doc->openDocument(url);
00260       setCaption(url.fileName(), false);
00261       fileOpenRecent->addURL( url );
00262     }
00263   }
00264 #endif
00265 
00266   slotStatusMsg(i18n("Ready."));
00267 }
00268 
00269 void KSync::slotFileOpenRecent(const KURL&)
00270 {
00271   slotStatusMsg(i18n("Opening file..."));
00272 
00273 #if 0   
00274   if(!doc->saveModified())
00275   {
00276      // here saving wasn't successful
00277   }
00278   else
00279   {
00280     doc->openDocument(url);
00281     setCaption(url.fileName(), false);
00282   }
00283 #endif
00284 
00285   slotStatusMsg(i18n("Ready."));
00286 }
00287 
00288 void KSync::slotFileSave()
00289 {
00290   slotStatusMsg(i18n("Saving file..."));
00291         
00292 //  doc->saveDocument(doc->URL());
00293 
00294   slotStatusMsg(i18n("Ready."));
00295 }
00296 
00297 void KSync::slotFileSaveAs()
00298 {
00299   slotStatusMsg(i18n("Saving file with a new filename..."));
00300 
00301   KURL url=KFileDialog::getSaveURL(QDir::currentDirPath(),
00302         i18n("*|All files"), this, i18n("Save As"));
00303   if(!url.isEmpty())
00304   {
00305 //    doc->saveDocument(url);
00306     fileOpenRecent->addURL(url);
00307 //    setCaption(url.fileName(),doc->isModified());
00308   }
00309 
00310   slotStatusMsg(i18n("Ready."));
00311 }
00312 
00313 void KSync::slotFileClose()
00314 {
00315   slotStatusMsg(i18n("Closing file..."));
00316         
00317   close();
00318 
00319   slotStatusMsg(i18n("Ready."));
00320 }
00321 
00322 void KSync::slotFilePrint()
00323 {
00324   slotStatusMsg(i18n("Printing..."));
00325 
00326   QPrinter printer;
00327   if (printer.setup(this))
00328   {
00329     mView->print(&printer);
00330   }
00331 
00332   slotStatusMsg(i18n("Ready."));
00333 }
00334 
00335 void KSync::slotFileQuit()
00336 {
00337   slotStatusMsg(i18n("Exiting..."));
00338   saveOptions();
00339   // close the first window, the list makes the next one the first again.
00340   // This ensures that queryClose() is called on each window to ask for closing
00341   KMainWindow* w;
00342   if(memberList)
00343   {
00344     for(w=memberList->first(); w!=0; w=memberList->first())
00345     {
00346       // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
00347       // the window and the application stay open.
00348       if(!w->close())
00349         break;
00350     }
00351   }     
00352   slotStatusMsg(i18n("Ready."));
00353 }
00354 
00355 void KSync::slotEditCut()
00356 {
00357   slotStatusMsg(i18n("Cutting selection..."));
00358 
00359   slotStatusMsg(i18n("Ready."));
00360 }
00361 
00362 void KSync::slotEditCopy()
00363 {
00364   slotStatusMsg(i18n("Copying selection to clipboard..."));
00365 
00366   slotStatusMsg(i18n("Ready."));
00367 }
00368 
00369 void KSync::slotEditPaste()
00370 {
00371   slotStatusMsg(i18n("Inserting clipboard contents..."));
00372 
00373   slotStatusMsg(i18n("Ready."));
00374 }
00375 
00376 void KSync::slotViewToolBar()
00377 {
00378   slotStatusMsg(i18n("Toggling toolbar..."));
00379 
00380   if(!viewToolBar->isChecked())
00381   {
00382     toolBar("mainToolBar")->hide();
00383   }
00384   else
00385   {
00386     toolBar("mainToolBar")->show();
00387   }             
00388 
00389   slotStatusMsg(i18n("Ready."));
00390 }
00391 
00392 void KSync::slotViewStatusBar()
00393 {
00394   slotStatusMsg(i18n("Toggle the statusbar..."));
00395 
00396   if(!viewStatusBar->isChecked())
00397   {
00398     statusBar()->hide();
00399   }
00400   else
00401   {
00402     statusBar()->show();
00403   }
00404 
00405   slotStatusMsg(i18n("Ready."));
00406 }
00407 
00408 
00409 void KSync::slotStatusMsg(const QString &text)
00410 {
00411   statusBar()->clear();
00412   statusBar()->changeItem(text, ID_STATUS_MSG);
00413 }
00414 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:17 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001