1
/*****************************************************************
3
Copyright (c) 2000 Matthias Elter <elter@kde.org>
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
of this software and associated documentation files (the "Software"), to deal
7
in the Software without restriction, including without limitation the rights
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the Software is
10
furnished to do so, subject to the following conditions:
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
******************************************************************/
26
#include <qstringlist.h>
27
#include <qpushbutton.h>
30
#include <kapplication.h>
34
#include <kcombobox.h>
35
#include <kurifilter.h>
38
#include <kmessagebox.h>
40
#include "runapplet.h"
41
#include "runapplet.moc"
45
KPanelApplet* init(QWidget *parent, const QString& configFile)
47
KGlobal::locale()->insertCatalogue("krunapplet");
48
return new RunApplet(configFile, KPanelApplet::Stretch, 0, parent, "krunapplet");
52
RunApplet::RunApplet(const QString& configFile, Type type, int actions,
53
QWidget *parent, const char *name)
54
: KPanelApplet(configFile, type, actions, parent, name)
56
// setBackgroundMode(X11ParentRelative);
57
setBackgroundOrigin( AncestorOrigin );
59
_label = new QLabel(i18n("Run command:"), this);
60
QFont f(_label->font());
62
// _label->setBackgroundMode(X11ParentRelative);
63
_label->setBackgroundOrigin( AncestorOrigin );
64
_label->setFixedHeight(14);
68
_btn = new QPushButton(this);
72
connect(_btn, SIGNAL(clicked()), SLOT(popup_combo()));
74
// setup history combo
75
_input = new KHistoryCombo(this);
78
_input->lineEdit()->installEventFilter( this );
79
connect(_input, SIGNAL(activated(const QString&)),
80
SLOT(run_command(const QString&)));
82
KConfig *c = config();
83
c->setGroup("General");
85
// restore history and completion list
86
QStringList list = c->readListEntry("Completion list");
87
_input->completionObject()->setItems(list);
88
list = c->readListEntry("History list");
89
_input->setHistoryItems(list);
90
int mode = c->readNumEntry( "CompletionMode", KGlobalSettings::completionMode() );
91
_input->setCompletionMode( (KGlobalSettings::Completion) mode );
93
_filterData = new KURIFilterData();
95
_hbox = new QHBox( 0, 0, WStyle_Customize | WType_Popup );
96
_hbox->setFixedSize(120, 22);
99
RunApplet::~RunApplet()
101
KConfig *c = config();
102
c->setGroup("General");
104
// save history and completion list
105
QStringList list = _input->completionObject()->items();
106
c->writeEntry("Completion list", list);
107
list = _input->historyItems();
108
c->writeEntry("History list", list);
109
c->writeEntry( "CompletionMode", (int) _input->completionMode() );
113
KGlobal::locale()->removeCatalogue("krunapplet");
117
bool RunApplet::eventFilter( QObject *o, QEvent * e)
119
if ( e->type() == QEvent::MouseButtonRelease ) {
122
return KPanelApplet::eventFilter( o, e );
125
void RunApplet::resizeEvent(QResizeEvent*)
127
if(orientation() == Horizontal)
130
_input->reparent(this, QPoint(0,0), true);
131
_label->setGeometry(0,0, width(), _label->height());
133
if(height() >= _input->sizeHint().height() + _label->height())
135
int inputVOffset = height() - _input->sizeHint().height() - 2;
136
int labelHeight = _label->sizeHint().height();
137
_label->setGeometry(0, inputVOffset - labelHeight,
138
width(), labelHeight);
139
_input->setGeometry(0, inputVOffset,
140
width(), _input->sizeHint().height());
147
// make it as high as the combobox naturally wants to be
148
// but no taller than the panel is!
149
// don't forget to center it vertically either.
150
int newHeight = _input->sizeHint().height();
151
if (newHeight > height())
152
newHeight = height();
153
_input->setGeometry(0, (height() - newHeight) / 2,
160
_btn->setFixedSize(width(), 22);
161
_input->reparent( _hbox, QPoint(0, 0), false);
167
void RunApplet::popupDirectionChange(KPanelApplet::Direction)
172
void RunApplet::setButtonText()
176
if (popupDirection() == Left)
194
int RunApplet::widthForHeight(int ) const
199
int RunApplet::heightForWidth(int ) const
204
void RunApplet::popup_combo()
207
if (popupDirection() == Left)
208
p = mapToGlobal(QPoint(-_input->width()-1, 0));
210
p = mapToGlobal(QPoint(width()+1, 0));
216
void RunApplet::run_command(const QString& command)
220
kapp->propagateSessionManager();
222
_filterData->setData( _input->currentText().stripWhiteSpace() );
224
filters << "kurisearchfilter" << "kshorturifilter";
225
KURIFilter::self()->filterURI( *(_filterData), filters );
227
_input->addToHistory(command);
230
QString cmd = (_filterData->uri().isLocalFile() ? _filterData->uri().path():_filterData->uri().url());
232
// Nothing interesting. Quit!
233
if ( cmd.isEmpty() ){
234
KMessageBox::sorry(0L, i18n("You have to enter a command to execute "
235
"or a URL to be opened first."));
239
else if (cmd == "logout")
241
bool shutdown = kapp->requestShutDown();
244
// This i18n string is in kdesktop/desktop.cc as well. Maybe we should DCOP to kdesktop instead ?
245
KMessageBox::error( 0, i18n("Unable to logout properly.\nThe session manager cannot "
246
"be contacted. You can try to force a shutdown by pressing "
247
"Ctrl+Alt+Backspace. Note, however, that your current "
248
"session will not be saved with a forced shutdown." ) );
255
switch( _filterData->uriType() )
257
case KURIFilterData::LOCAL_FILE:
258
case KURIFilterData::LOCAL_DIR:
259
case KURIFilterData::NET_PROTOCOL:
260
case KURIFilterData::HELP:
262
(void) new KRun( _filterData->uri() );
265
case KURIFilterData::EXECUTABLE:
266
case KURIFilterData::SHELL:
269
if( _filterData->hasArgsAndOptions() )
270
cmd += _filterData->argsAndOptions();
273
case KURIFilterData::UNKNOWN:
274
case KURIFilterData::ERROR:
276
KMessageBox::sorry( 0, i18n("<qt>The program name or command <b>%1</b>\n"
277
"cannot be found. Please correct the command\n"
278
"or URL and try again</qt>").arg( cmd ) );
279
_input->removeFromHistory( _input->currentText() );
284
if (KRun::runCommand( cmd, exec, "" ))
288
KMessageBox::sorry( 0, i18n("<qt>Could not run <b>%1</b>.\nPlease correct"
289
" the command or URL and try again.</qt>").arg( cmd ) );
290
_input->removeFromHistory( _input->currentText() );
298
if (orientation() == Vertical)