kandy.cpp
00001 /* 00002 This file is part of Kandy. 00003 00004 Copyright (c) 2000,2001,2002 Cornelius Schumacher <schumacher@kde.org> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 As a special exception, permission is given to link this program 00021 with any edition of TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include <tqlineedit.h> 00026 #include <tqprinter.h> 00027 #include <tqprintdialog.h> 00028 #include <tqpainter.h> 00029 #include <tqpaintdevicemetrics.h> 00030 00031 #include <kglobal.h> 00032 #include <klocale.h> 00033 #include <kiconloader.h> 00034 #include <kmenubar.h> 00035 #include <kkeydialog.h> 00036 #include <kaccel.h> 00037 #include <kio/netaccess.h> 00038 #include <kfiledialog.h> 00039 #include <kconfig.h> 00040 #include <kurl.h> 00041 #include <kurlrequesterdlg.h> 00042 #include <kdebug.h> 00043 #include <kmessagebox.h> 00044 #include <kstandarddirs.h> 00045 #include <kedittoolbar.h> 00046 #include <kstdaccel.h> 00047 #include <kaction.h> 00048 #include <kstdaction.h> 00049 #include <kurldrag.h> 00050 00051 #include "kandyprefsdialog.h" 00052 #include "commandscheduler.h" 00053 #include "kandyprefs.h" 00054 #include "modem.h" 00055 00056 #include "kandy.h" 00057 #include <kstatusbar.h> 00058 #include "kandy.moc" 00059 00060 Kandy::Kandy(CommandScheduler *scheduler) 00061 : KMainWindow( 0, "Kandy" ), 00062 mPrinter(0) 00063 { 00064 mScheduler = scheduler; 00065 00066 mPreferencesDialog = 0; 00067 00068 mView = new KandyView(mScheduler,this); 00069 00070 // accept dnd 00071 setAcceptDrops(true); 00072 00073 // tell the KMainWindow that this is indeed the main widget 00074 setCentralWidget(mView); 00075 00076 // then, setup our actions 00077 setupActions(); 00078 00079 statusBar()->insertItem(i18n(" Disconnected "),0,0,true); 00080 00081 setAutoSaveSettings(); 00082 00083 // allow the view to change the statusbar and caption 00084 connect(mView, TQT_SIGNAL(signalChangeStatusbar(const TQString&)), 00085 this, TQT_SLOT(changeStatusbar(const TQString&))); 00086 connect(mView, TQT_SIGNAL(signalChangeCaption(const TQString&)), 00087 this, TQT_SLOT(changeCaption(const TQString&))); 00088 00089 connect(mView,TQT_SIGNAL(modifiedChanged(bool)),TQT_SLOT(setTitle())); 00090 00091 KConfig *config = KGlobal::config(); 00092 config->setGroup("General"); 00093 TQString currentProfile = config->readEntry("CurrentProfile", 00094 locate("appdata","default.kandy")); 00095 if (!currentProfile.isEmpty()) load(currentProfile); 00096 } 00097 00098 Kandy::~Kandy() 00099 { 00100 } 00101 00102 void Kandy::load(const TQString& filename) 00103 { 00104 if (!mView->loadFile(filename)) { 00105 KMessageBox::error(this,i18n("Could not load file %1").arg(filename)); 00106 } 00107 00108 mFilename = filename; 00109 setTitle(); 00110 } 00111 00112 void Kandy::save(const TQString & filename) 00113 { 00114 if (!filename.isEmpty()) { 00115 if (!mView->saveFile(filename)) { 00116 KMessageBox::error(this,i18n("Could not save file %1.").arg(filename)); 00117 } else { 00118 mFilename = filename; 00119 setTitle(); 00120 } 00121 } 00122 } 00123 00124 void Kandy::setupActions() 00125 { 00126 KStdAction::open(TQT_TQOBJECT(this), TQT_SLOT(fileOpen()), actionCollection()); 00127 KStdAction::save(TQT_TQOBJECT(this), TQT_SLOT(fileSave()), actionCollection()); 00128 KStdAction::saveAs(TQT_TQOBJECT(this), TQT_SLOT(fileSaveAs()), actionCollection()); 00129 // KStdAction::print(TQT_TQOBJECT(this), TQT_SLOT(filePrint()), actionCollection()); 00130 KStdAction::quit(TQT_TQOBJECT(this), TQT_SLOT(close()), actionCollection()); 00131 00132 createStandardStatusBarAction(); 00133 setStandardToolBarMenuEnabled(true); 00134 00135 KStdAction::keyBindings(TQT_TQOBJECT(this), TQT_SLOT(optionsConfigureKeys()), actionCollection()); 00136 KStdAction::configureToolbars(TQT_TQOBJECT(this), TQT_SLOT(optionsConfigureToolbars()), actionCollection()); 00137 KStdAction::preferences(TQT_TQOBJECT(this), TQT_SLOT(optionsPreferences()), actionCollection()); 00138 00139 new KAction(i18n("Mobile GUI"),0,TQT_TQOBJECT(this),TQT_SLOT(showMobileGui()), 00140 actionCollection(),"show_mobilegui"); 00141 00142 mConnectAction = new KAction(i18n("Connect"),0,TQT_TQOBJECT(this),TQT_SLOT(modemConnect()), 00143 actionCollection(),"modem_connect"); 00144 mDisconnectAction = new KAction(i18n("Disconnect"),0,TQT_TQOBJECT(this), 00145 TQT_SLOT(modemDisconnect()),actionCollection(), 00146 "modem_disconnect"); 00147 00148 createGUI(); 00149 } 00150 00151 void Kandy::saveProperties(KConfig */*config*/) 00152 { 00153 // the 'config' object points to the session managed 00154 // config file. anything you write here will be available 00155 // later when this app is restored 00156 } 00157 00158 void Kandy::readProperties(KConfig */*config*/) 00159 { 00160 // the 'config' object points to the session managed 00161 // config file. this function is automatically called whenever 00162 // the app is being restored. read in here whatever you wrote 00163 // in 'saveProperties' 00164 } 00165 00166 void Kandy::dragEnterEvent(TQDragEnterEvent *event) 00167 { 00168 // do nothing 00169 KMainWindow::dragEnterEvent(event); 00170 00171 // accept uri drops only 00172 // event->accept(KURLDrag::canDecode(event)); 00173 } 00174 00175 void Kandy::dropEvent(TQDropEvent *event) 00176 { 00177 // this is a very simplistic implementation of a drop event. we 00178 // will only accept a dropped URL. the TQt dnd code can do *much* 00179 // much more, so please read the docs there 00180 00181 // do nothing 00182 KMainWindow::dropEvent(event); 00183 /* 00184 KURL::List list; 00185 00186 // see if we can decode a URI.. if not, just ignore it 00187 if (KURLDrag::decode(event, list) && !list.isEmpty()) 00188 { 00189 // okay, we have a URI.. process it 00190 const KURL &url = uri.first(); 00191 00192 if (url.isLocalFile()) 00193 { 00194 // load in the file 00195 load(url.path()); 00196 } 00197 } 00198 */ 00199 } 00200 00201 void Kandy::fileOpen() 00202 { 00203 // this slot is called whenever the File->Open menu is selected, 00204 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar 00205 // button is clicked 00206 TQString filename = KFileDialog::getOpenFileName(); 00207 if (!filename.isEmpty()) load(filename); 00208 } 00209 00210 void Kandy::fileSave() 00211 { 00212 if (mFilename.isEmpty()) fileSaveAs(); 00213 else save(mFilename); 00214 } 00215 00216 void Kandy::fileSaveAs() 00217 { 00218 TQString filename = KFileDialog::getSaveFileName(); 00219 save(filename); 00220 } 00221 00222 void Kandy::filePrint() 00223 { 00224 // this slot is called whenever the File->Print menu is selected, 00225 // the Print shortcut is pressed (usually CTRL+P) or the Print toolbar 00226 // button is clicked 00227 if (!mPrinter) mPrinter = new TQPrinter; 00228 if (TQPrintDialog::getPrinterSetup(mPrinter)) 00229 { 00230 // setup the printer. with TQt, you always "print" to a 00231 // TQPainter.. whether the output medium is a pixmap, a screen, 00232 // or paper 00233 TQPainter p; 00234 p.begin(mPrinter); 00235 00236 // we let our view do the actual printing 00237 TQPaintDeviceMetrics metrics(mPrinter); 00238 mView->print(&p, metrics.height(), metrics.width()); 00239 00240 // and send the result to the printer 00241 p.end(); 00242 } 00243 } 00244 00245 void Kandy::optionsConfigureKeys() 00246 { 00247 KKeyDialog::configure( actionCollection(), this ); 00248 } 00249 00250 void Kandy::optionsConfigureToolbars() 00251 { 00252 // use the standard toolbar editor 00253 saveMainWindowSettings( KGlobal::config(), autoSaveGroup() ); 00254 KEditToolbar dlg(actionCollection()); 00255 connect(&dlg, TQT_SIGNAL(newToolbarConfig()), this, TQT_SLOT(newToolbarConfig())); 00256 dlg.exec(); 00257 } 00258 00259 void Kandy::newToolbarConfig() 00260 { 00261 // this slot is called when user clicks "Ok" or "Apply" in the toolbar editor. 00262 // recreate our GUI, and re-apply the settings (e.g. "text under icons", etc.) 00263 createGUI(); 00264 applyMainWindowSettings( KGlobal::config(), autoSaveGroup() ); 00265 } 00266 00267 void Kandy::optionsPreferences() 00268 { 00269 if (!mPreferencesDialog) { 00270 mPreferencesDialog = new KandyPrefsDialog(this); 00271 mPreferencesDialog->readConfig(); 00272 } 00273 00274 mPreferencesDialog->show(); 00275 mPreferencesDialog->raise(); 00276 } 00277 00278 void Kandy::changeStatusbar(const TQString& text) 00279 { 00280 // display the text on the statusbar 00281 statusBar()->message(text); 00282 } 00283 00284 void Kandy::changeCaption(const TQString& text) 00285 { 00286 // display the text on the caption 00287 setCaption(text); 00288 } 00289 00290 void Kandy::setTitle() 00291 { 00292 if (mFilename.isEmpty()) { 00293 setCaption(i18n("New Profile"),mView->isModified()); 00294 } else { 00295 setCaption(mFilename,mView->isModified()); 00296 } 00297 } 00298 00299 bool Kandy::queryClose() 00300 { 00301 if (mView->isModified()) { 00302 switch (KMessageBox::warningYesNoCancel(this, 00303 i18n("Save changes to profile %1?").arg(mFilename), TQString(), KStdGuiItem::save(), KStdGuiItem::discard())) { 00304 case KMessageBox::Yes : 00305 fileSave(); 00306 return true; 00307 case KMessageBox::No : 00308 return true; 00309 default: // cancel 00310 return false; 00311 } 00312 } else { 00313 return true; 00314 } 00315 } 00316 00317 void Kandy::modemConnect() 00318 { 00319 if (!mScheduler->modem()->open()) { 00320 KMessageBox::sorry(this, 00321 i18n("Cannot open modem device %1.") 00322 .arg(KandyPrefs::serialDevice()), i18n("Modem Error")); 00323 return; 00324 } 00325 00326 statusBar()->changeItem(i18n(" Connected "),0); 00327 00328 emit connectStateChanged(true); 00329 } 00330 00331 void Kandy::modemDisconnect() 00332 { 00333 mScheduler->modem()->close(); 00334 00335 statusBar()->changeItem(i18n(" Disconnected "),0); 00336 00337 emit connectStateChanged(false); 00338 } 00339 00340 void Kandy::showMobileGui() 00341 { 00342 emit showMobileWin(); 00343 } 00344 00345 void Kandy::showErrorMessage( const TQString &text ) 00346 { 00347 KMessageBox::error( 0, text ); 00348 }