kandyview.cpp
00001 /* 00002 This file is part of Kandy. 00003 00004 Copyright (c) 2001 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 <unistd.h> 00026 00027 #include <tqpainter.h> 00028 #include <tqlayout.h> 00029 #include <tqhbox.h> 00030 #include <tqvbox.h> 00031 #include <tqtextedit.h> 00032 #include <tqlistview.h> 00033 #include <tqdom.h> 00034 #include <tqtextstream.h> 00035 #include <tqfile.h> 00036 #include <tqlineedit.h> 00037 #include <tqcheckbox.h> 00038 #include <tqlabel.h> 00039 #include <tqpushbutton.h> 00040 00041 #include <kurl.h> 00042 #include <tdemessagebox.h> 00043 #include <kdebug.h> 00044 #include <tdelocale.h> 00045 #include <tdeglobal.h> 00046 #include <tdeconfig.h> 00047 #include <kinputdialog.h> 00048 #include <kdialog.h> 00049 00050 #include "modem.h" 00051 #include "cmdpropertiesdialog.h" 00052 #include "commanditem.h" 00053 #include "atcommand.h" 00054 #include "commandscheduler.h" 00055 #include "kandyprefs.h" 00056 00057 #include "kandyview.h" 00058 #include "kandyview.moc" 00059 00060 KandyView::KandyView(CommandScheduler *scheduler,TQWidget *parent) 00061 : TQWidget(parent) 00062 { 00063 mModified = false; 00064 mScheduler = scheduler; 00065 00066 TQBoxLayout *topLayout = new TQVBoxLayout( this ); 00067 00068 TQSplitter *mainSplitter = new TQSplitter( Qt::Horizontal, this ); 00069 topLayout->addWidget( mainSplitter ); 00070 00071 TQWidget *commandBox = new TQWidget( mainSplitter ); 00072 00073 TQBoxLayout *commandLayout = new TQVBoxLayout( commandBox ); 00074 commandLayout->setMargin( KDialog::marginHint() ); 00075 commandLayout->setSpacing( KDialog::spacingHint() ); 00076 00077 mCommandList = new TQListView( commandBox ); 00078 mCommandList->addColumn( i18n( "Name" ) ); 00079 mCommandList->addColumn( i18n( "Command" ) ); 00080 mCommandList->addColumn( i18n( "Hex" ) ); 00081 commandLayout->addWidget( mCommandList ); 00082 00083 connect( mCommandList, TQT_SIGNAL( doubleClicked(TQListViewItem*) ), 00084 TQT_SLOT( executeCommand() ) ); 00085 00086 TQPushButton *buttonAdd = new TQPushButton( i18n("Add..."), commandBox ); 00087 commandLayout->addWidget( buttonAdd ); 00088 connect( buttonAdd, TQT_SIGNAL( clicked() ), TQT_SLOT( addCommand() ) ); 00089 00090 TQPushButton *buttonEdit = new TQPushButton( i18n("Edit..."), commandBox ); 00091 commandLayout->addWidget( buttonEdit ); 00092 connect( buttonEdit, TQT_SIGNAL( clicked() ), TQT_SLOT( editCommand() ) ); 00093 00094 TQPushButton *buttonDelete = new TQPushButton( i18n("Delete"), commandBox ); 00095 commandLayout->addWidget( buttonDelete ); 00096 connect( buttonDelete, TQT_SIGNAL( clicked() ), TQT_SLOT( deleteCommand() ) ); 00097 00098 TQPushButton *buttonExecute = new TQPushButton( i18n("Execute"), commandBox ); 00099 commandLayout->addWidget( buttonExecute ); 00100 connect( buttonExecute, TQT_SIGNAL( clicked() ), TQT_SLOT( executeCommand() ) ); 00101 00102 TQSplitter *ioSplitter = new TQSplitter( Qt::Vertical, mainSplitter ); 00103 00104 TQWidget *inBox = new TQWidget( ioSplitter ); 00105 00106 TQBoxLayout *inLayout = new TQVBoxLayout( inBox ); 00107 00108 TQLabel *inLabel = new TQLabel( i18n("Input:"), inBox ); 00109 inLabel->setMargin( 2 ); 00110 inLayout->addWidget( inLabel ); 00111 00112 mInput = new TQTextEdit( inBox ); 00113 inLayout->addWidget( mInput ); 00114 00115 TQWidget *outBox = new TQWidget( ioSplitter ); 00116 00117 TQBoxLayout *outLayout = new TQVBoxLayout( outBox ); 00118 00119 TQLabel *outLabel = new TQLabel( i18n( "Output:"), outBox ); 00120 outLabel->setMargin( 2 ); 00121 outLayout->addWidget( outLabel ); 00122 00123 mOutput = new TQTextEdit( outBox ); 00124 mOutput->setReadOnly( true ); 00125 outLayout->addWidget( mOutput ); 00126 00127 TQVBox *resultBox = new TQVBox( mainSplitter ); 00128 00129 TQLabel *resultLabel = new TQLabel( i18n("Result:"), resultBox ); 00130 resultLabel->setMargin( 2 ); 00131 00132 mResultView = new TQTextEdit( resultBox ); 00133 mResultView->setReadOnly( true ); 00134 00135 connect (mInput,TQT_SIGNAL(returnPressed()),TQT_SLOT(processLastLine())); 00136 00137 connect(mScheduler->modem(),TQT_SIGNAL(gotLine(const char *)), 00138 TQT_SLOT(appendOutput(const char *))); 00139 00140 connect(mScheduler,TQT_SIGNAL(result(const TQString &)), 00141 mResultView,TQT_SLOT(setText(const TQString &))); 00142 connect(mScheduler,TQT_SIGNAL(commandProcessed(ATCommand *)), 00143 TQT_SLOT(setResult(ATCommand *))); 00144 } 00145 00146 KandyView::~KandyView() 00147 { 00148 } 00149 00150 00151 void KandyView::print(TQPainter *, int, int) 00152 { 00153 // do the actual printing, here 00154 // p->drawText(etc..) 00155 } 00156 00157 void KandyView::importPhonebook() 00158 { 00159 #if 0 00160 createMobileGui(); 00161 connect (mMobileGui,TQT_SIGNAL(phonebookRead()),mMobileGui,TQT_SLOT(writeKab())); 00162 mMobileGui->readPhonebook(); 00163 #endif 00164 } 00165 00166 void KandyView::slotSetTitle(const TQString& title) 00167 { 00168 emit signalChangeCaption(title); 00169 } 00170 00171 void KandyView::processLastLine() 00172 { 00173 int para = 0; 00174 int row = 0; 00175 mInput->getCursorPosition( ¶, &row ); 00176 00177 if ( para > 0 ) { 00178 mLastInput = mInput->text( para - 1 ); 00179 00180 kdDebug(5960) << "processLastLine(): " << mLastInput << endl; 00181 00182 mScheduler->execute(mLastInput); 00183 } 00184 } 00185 00186 void KandyView::appendOutput(const char *line) 00187 { 00188 // kdDebug(5960) << "OUT: " << line << endl; 00189 mOutput->append(line); 00190 mOutput->setCursorPosition(mOutput->paragraphs()-1,0); 00191 } 00192 00193 void KandyView::setResult(ATCommand *command) 00194 { 00195 if (command == 0) { 00196 kdDebug(5960) << "KandyView::setResult(): Error! No command." << endl; 00197 mResultView->setText(i18n("Error")); 00198 return; 00199 } 00200 00201 // kdDebug(5960) << "KandyView::setResult(): " << endl << mResult << endl 00202 // << mLastCommand->processOutput(mResult) << endl; 00203 00204 mResultView->setText(command->cmdName() + ":\n" + command->processOutput()); 00205 } 00206 00207 void KandyView::addCommand() 00208 { 00209 ATCommand *cmd = new ATCommand(mLastInput); 00210 00211 CmdPropertiesDialog *dlg = new CmdPropertiesDialog(cmd,this,"cmdprop",true); 00212 00213 int result = dlg->exec(); 00214 00215 if (result == TQDialog::Accepted) { 00216 new CommandItem(mCommandList,cmd); 00217 mScheduler->commandSet()->addCommand(cmd); 00218 setModified(); 00219 } else { 00220 delete cmd; 00221 } 00222 } 00223 00224 void KandyView::editCommand() 00225 { 00226 TQListViewItem *item = mCommandList->currentItem(); 00227 if (item) { 00228 CommandItem *cmdItem = (CommandItem *)item; 00229 ATCommand *cmd = cmdItem->command(); 00230 00231 CmdPropertiesDialog *dlg = new CmdPropertiesDialog(cmd,this,"cmdprop",true); 00232 00233 int result = dlg->exec(); 00234 00235 if (result == TQDialog::Accepted) { 00236 cmdItem->setItemText(); 00237 setModified(); 00238 } 00239 } 00240 } 00241 00242 void KandyView::executeCommand() 00243 { 00244 CommandItem *item = (CommandItem *)(mCommandList->currentItem()); 00245 if (item) { 00246 ATCommand *cmd = item->command(); 00247 TQPtrList<ATParameter> paraList = cmd->parameters(); 00248 for(uint i=0;i<paraList.count();++i) { 00249 ATParameter *p = paraList.at(i); 00250 if (p->userInput()) { 00251 bool ok = false; 00252 TQString value = KInputDialog::getText(TQString(), 00253 i18n("Enter value for %1:").arg(p->name()),TQString(),&ok,this); 00254 if (!ok) 00255 return; 00256 p->setValue(value); 00257 } 00258 } 00259 kdDebug(5960) << "KandyView::executeCommand(): " << cmd->cmd() << endl; 00260 mScheduler->execute(cmd); 00261 } 00262 } 00263 00264 void KandyView::deleteCommand() 00265 { 00266 CommandItem *item = dynamic_cast<CommandItem *>(mCommandList->currentItem()); 00267 if (item) { 00268 mScheduler->commandSet()->deleteCommand(item->command()); 00269 delete item; 00270 setModified(); 00271 } 00272 } 00273 00274 bool KandyView::loadFile(const TQString& filename) 00275 { 00276 mCommandList->clear(); 00277 00278 if (!mScheduler->loadProfile(filename)) return false; 00279 00280 TQPtrList<ATCommand> *cmds = mScheduler->commandSet()->commandList(); 00281 00282 for(uint i=0;i<cmds->count();++i) { 00283 new CommandItem(mCommandList,cmds->at(i)); 00284 } 00285 00286 TDEConfig *config = TDEGlobal::config(); 00287 config->setGroup("General"); 00288 config->writeEntry("CurrentProfile",filename); 00289 00290 setModified(false); 00291 00292 return true; 00293 } 00294 00295 bool KandyView::saveFile(const TQString& filename) 00296 { 00297 if (!mScheduler->saveProfile(filename)) return false; 00298 00299 TDEConfig *config = TDEGlobal::config(); 00300 config->setGroup("General"); 00301 config->writeEntry("CurrentProfile",filename); 00302 00303 setModified(false); 00304 00305 return true; 00306 } 00307 00308 void KandyView::setModified(bool modified) 00309 { 00310 if (modified != mModified) { 00311 mModified = modified; 00312 emit modifiedChanged(mModified); 00313 } 00314 } 00315 00316 bool KandyView::isModified() 00317 { 00318 return mModified; 00319 }