kandy

kandyview.cpp
1/*
2 This file is part of Kandy.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include <unistd.h>
26
27#include <tqpainter.h>
28#include <tqlayout.h>
29#include <tqhbox.h>
30#include <tqvbox.h>
31#include <tqtextedit.h>
32#include <tqlistview.h>
33#include <tqdom.h>
34#include <tqtextstream.h>
35#include <tqfile.h>
36#include <tqlineedit.h>
37#include <tqcheckbox.h>
38#include <tqlabel.h>
39#include <tqpushbutton.h>
40
41#include <kurl.h>
42#include <tdemessagebox.h>
43#include <kdebug.h>
44#include <tdelocale.h>
45#include <tdeglobal.h>
46#include <tdeconfig.h>
47#include <kinputdialog.h>
48#include <kdialog.h>
49
50#include "modem.h"
51#include "cmdpropertiesdialog.h"
52#include "commanditem.h"
53#include "atcommand.h"
54#include "commandscheduler.h"
55#include "kandyprefs.h"
56
57#include "kandyview.h"
58#include "kandyview.moc"
59
60KandyView::KandyView(CommandScheduler *scheduler,TQWidget *parent)
61 : TQWidget(parent)
62{
63 mModified = false;
64 mScheduler = scheduler;
65
66 TQBoxLayout *topLayout = new TQVBoxLayout( this );
67
68 TQSplitter *mainSplitter = new TQSplitter( TQt::Horizontal, this );
69 topLayout->addWidget( mainSplitter );
70
71 TQWidget *commandBox = new TQWidget( mainSplitter );
72
73 TQBoxLayout *commandLayout = new TQVBoxLayout( commandBox );
74 commandLayout->setMargin( KDialog::marginHint() );
75 commandLayout->setSpacing( KDialog::spacingHint() );
76
77 mCommandList = new TQListView( commandBox );
78 mCommandList->addColumn( i18n( "Name" ) );
79 mCommandList->addColumn( i18n( "Command" ) );
80 mCommandList->addColumn( i18n( "Hex" ) );
81 commandLayout->addWidget( mCommandList );
82
83 connect( mCommandList, TQ_SIGNAL( doubleClicked(TQListViewItem*) ),
84 TQ_SLOT( executeCommand() ) );
85
86 TQPushButton *buttonAdd = new TQPushButton( i18n("Add..."), commandBox );
87 commandLayout->addWidget( buttonAdd );
88 connect( buttonAdd, TQ_SIGNAL( clicked() ), TQ_SLOT( addCommand() ) );
89
90 TQPushButton *buttonEdit = new TQPushButton( i18n("Edit..."), commandBox );
91 commandLayout->addWidget( buttonEdit );
92 connect( buttonEdit, TQ_SIGNAL( clicked() ), TQ_SLOT( editCommand() ) );
93
94 TQPushButton *buttonDelete = new TQPushButton( i18n("Delete"), commandBox );
95 commandLayout->addWidget( buttonDelete );
96 connect( buttonDelete, TQ_SIGNAL( clicked() ), TQ_SLOT( deleteCommand() ) );
97
98 TQPushButton *buttonExecute = new TQPushButton( i18n("Execute"), commandBox );
99 commandLayout->addWidget( buttonExecute );
100 connect( buttonExecute, TQ_SIGNAL( clicked() ), TQ_SLOT( executeCommand() ) );
101
102 TQSplitter *ioSplitter = new TQSplitter( TQt::Vertical, mainSplitter );
103
104 TQWidget *inBox = new TQWidget( ioSplitter );
105
106 TQBoxLayout *inLayout = new TQVBoxLayout( inBox );
107
108 TQLabel *inLabel = new TQLabel( i18n("Input:"), inBox );
109 inLabel->setMargin( 2 );
110 inLayout->addWidget( inLabel );
111
112 mInput = new TQTextEdit( inBox );
113 inLayout->addWidget( mInput );
114
115 TQWidget *outBox = new TQWidget( ioSplitter );
116
117 TQBoxLayout *outLayout = new TQVBoxLayout( outBox );
118
119 TQLabel *outLabel = new TQLabel( i18n( "Output:"), outBox );
120 outLabel->setMargin( 2 );
121 outLayout->addWidget( outLabel );
122
123 mOutput = new TQTextEdit( outBox );
124 mOutput->setReadOnly( true );
125 outLayout->addWidget( mOutput );
126
127 TQVBox *resultBox = new TQVBox( mainSplitter );
128
129 TQLabel *resultLabel = new TQLabel( i18n("Result:"), resultBox );
130 resultLabel->setMargin( 2 );
131
132 mResultView = new TQTextEdit( resultBox );
133 mResultView->setReadOnly( true );
134
135 connect (mInput,TQ_SIGNAL(returnPressed()),TQ_SLOT(processLastLine()));
136
137 connect(mScheduler->modem(),TQ_SIGNAL(gotLine(const char *)),
138 TQ_SLOT(appendOutput(const char *)));
139
140 connect(mScheduler,TQ_SIGNAL(result(const TQString &)),
141 mResultView,TQ_SLOT(setText(const TQString &)));
142 connect(mScheduler,TQ_SIGNAL(commandProcessed(ATCommand *)),
143 TQ_SLOT(setResult(ATCommand *)));
144}
145
147{
148}
149
150
151void KandyView::print(TQPainter *, int, int)
152{
153 // do the actual printing, here
154 // p->drawText(etc..)
155}
156
158{
159#if 0
160 createMobileGui();
161 connect (mMobileGui,TQ_SIGNAL(phonebookRead()),mMobileGui,TQ_SLOT(writeKab()));
162 mMobileGui->readPhonebook();
163#endif
164}
165
166void KandyView::slotSetTitle(const TQString& title)
167{
168 emit signalChangeCaption(title);
169}
170
171void KandyView::processLastLine()
172{
173 int para = 0;
174 int row = 0;
175 mInput->getCursorPosition( &para, &row );
176
177 if ( para > 0 ) {
178 mLastInput = mInput->text( para - 1 );
179
180 kdDebug(5960) << "processLastLine(): " << mLastInput << endl;
181
182 mScheduler->execute(mLastInput);
183 }
184}
185
186void KandyView::appendOutput(const char *line)
187{
188// kdDebug(5960) << "OUT: " << line << endl;
189 mOutput->append(line);
190 mOutput->setCursorPosition(mOutput->paragraphs()-1,0);
191}
192
193void KandyView::setResult(ATCommand *command)
194{
195 if (command == 0) {
196 kdDebug(5960) << "KandyView::setResult(): Error! No command." << endl;
197 mResultView->setText(i18n("Error"));
198 return;
199 }
200
201// kdDebug(5960) << "KandyView::setResult(): " << endl << mResult << endl
202// << mLastCommand->processOutput(mResult) << endl;
203
204 mResultView->setText(command->cmdName() + ":\n" + command->processOutput());
205}
206
207void KandyView::addCommand()
208{
209 ATCommand *cmd = new ATCommand(mLastInput);
210
211 CmdPropertiesDialog *dlg = new CmdPropertiesDialog(cmd,this,"cmdprop",true);
212
213 int result = dlg->exec();
214
215 if (result == TQDialog::Accepted) {
216 new CommandItem(mCommandList,cmd);
217 mScheduler->commandSet()->addCommand(cmd);
218 setModified();
219 } else {
220 delete cmd;
221 }
222}
223
224void KandyView::editCommand()
225{
226 TQListViewItem *item = mCommandList->currentItem();
227 if (item) {
228 CommandItem *cmdItem = (CommandItem *)item;
229 ATCommand *cmd = cmdItem->command();
230
231 CmdPropertiesDialog *dlg = new CmdPropertiesDialog(cmd,this,"cmdprop",true);
232
233 int result = dlg->exec();
234
235 if (result == TQDialog::Accepted) {
236 cmdItem->setItemText();
237 setModified();
238 }
239 }
240}
241
242void KandyView::executeCommand()
243{
244 CommandItem *item = (CommandItem *)(mCommandList->currentItem());
245 if (item) {
246 ATCommand *cmd = item->command();
247 TQPtrList<ATParameter> paraList = cmd->parameters();
248 for(uint i=0;i<paraList.count();++i) {
249 ATParameter *p = paraList.at(i);
250 if (p->userInput()) {
251 bool ok = false;
252 TQString value = KInputDialog::getText(TQString(),
253 i18n("Enter value for %1:").arg(p->name()),TQString(),&ok,this);
254 if (!ok)
255 return;
256 p->setValue(value);
257 }
258 }
259 kdDebug(5960) << "KandyView::executeCommand(): " << cmd->cmd() << endl;
260 mScheduler->execute(cmd);
261 }
262}
263
264void KandyView::deleteCommand()
265{
266 CommandItem *item = dynamic_cast<CommandItem *>(mCommandList->currentItem());
267 if (item) {
268 mScheduler->commandSet()->deleteCommand(item->command());
269 delete item;
270 setModified();
271 }
272}
273
274bool KandyView::loadFile(const TQString& filename)
275{
276 mCommandList->clear();
277
278 if (!mScheduler->loadProfile(filename)) return false;
279
280 TQPtrList<ATCommand> *cmds = mScheduler->commandSet()->commandList();
281
282 for(uint i=0;i<cmds->count();++i) {
283 new CommandItem(mCommandList,cmds->at(i));
284 }
285
286 TDEConfig *config = TDEGlobal::config();
287 config->setGroup("General");
288 config->writeEntry("CurrentProfile",filename);
289
290 setModified(false);
291
292 return true;
293}
294
295bool KandyView::saveFile(const TQString& filename)
296{
297 if (!mScheduler->saveProfile(filename)) return false;
298
299 TDEConfig *config = TDEGlobal::config();
300 config->setGroup("General");
301 config->writeEntry("CurrentProfile",filename);
302
303 setModified(false);
304
305 return true;
306}
307
308void KandyView::setModified(bool modified)
309{
310 if (modified != mModified) {
311 mModified = modified;
312 emit modifiedChanged(mModified);
313 }
314}
315
316bool KandyView::isModified()
317{
318 return mModified;
319}
This class provides an abstraction of an AT command.
Definition: atcommand.h:54
TQListView item representing a modem command.
Definition: commanditem.h:38
void signalChangeCaption(const TQString &text)
Use this signal to change the content of the caption.
void print(TQPainter *, int height, int width)
Print this view to any medium – paper or not.
Definition: kandyview.cpp:151
void importPhonebook()
Import phonebook from mobile phone and save it to Kab.
Definition: kandyview.cpp:157
virtual ~KandyView()
Destructor.
Definition: kandyview.cpp:146
KandyView(CommandScheduler *, TQWidget *parent)
Default constructor.
Definition: kandyview.cpp:60