~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to kicker/applets/run/runapplet.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright (c) 2000 Matthias Elter <elter@kde.org>
 
4
 
 
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:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
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.
 
21
 
 
22
******************************************************************/
 
23
 
 
24
#include <qlabel.h>
 
25
#include <qfont.h>
 
26
#include <qstringlist.h>
 
27
#include <qpushbutton.h>
 
28
#include <qhbox.h>
 
29
 
 
30
#include <kapplication.h>
 
31
#include <kglobal.h>
 
32
#include <klocale.h>
 
33
#include <kconfig.h>
 
34
#include <kcombobox.h>
 
35
#include <kurifilter.h>
 
36
#include <kdialog.h>
 
37
#include <krun.h>
 
38
#include <kmessagebox.h>
 
39
 
 
40
#include "runapplet.h"
 
41
#include "runapplet.moc"
 
42
 
 
43
extern "C"
 
44
{
 
45
  KPanelApplet* init(QWidget *parent, const QString& configFile)
 
46
  {
 
47
    KGlobal::locale()->insertCatalogue("krunapplet");
 
48
    return new RunApplet(configFile, KPanelApplet::Stretch, 0, parent, "krunapplet");
 
49
  }
 
50
}
 
51
 
 
52
RunApplet::RunApplet(const QString& configFile, Type type, int actions,
 
53
                             QWidget *parent, const char *name)
 
54
  : KPanelApplet(configFile, type, actions, parent, name)
 
55
{
 
56
  //  setBackgroundMode(X11ParentRelative);
 
57
    setBackgroundOrigin( AncestorOrigin );
 
58
    // setup label
 
59
    _label = new QLabel(i18n("Run command:"), this);
 
60
    QFont f(_label->font());
 
61
    f.setPixelSize(12);
 
62
//    _label->setBackgroundMode(X11ParentRelative);
 
63
    _label->setBackgroundOrigin( AncestorOrigin );
 
64
    _label->setFixedHeight(14);
 
65
    _label->setFont(f);
 
66
 
 
67
    // setup popup button
 
68
    _btn = new QPushButton(this);
 
69
    f = _btn->font();
 
70
    f.setPixelSize(12);
 
71
    _btn->setFont(f);
 
72
    connect(_btn, SIGNAL(clicked()), SLOT(popup_combo()));
 
73
 
 
74
    // setup history combo
 
75
    _input = new KHistoryCombo(this);
 
76
    _input->setFocus();
 
77
    _input->clearEdit();
 
78
    _input->lineEdit()->installEventFilter( this );
 
79
    connect(_input, SIGNAL(activated(const QString&)),
 
80
            SLOT(run_command(const QString&)));
 
81
 
 
82
    KConfig *c = config();
 
83
    c->setGroup("General");
 
84
 
 
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 );
 
92
 
 
93
    _filterData = new KURIFilterData();
 
94
 
 
95
    _hbox = new QHBox( 0, 0, WStyle_Customize | WType_Popup );
 
96
    _hbox->setFixedSize(120, 22);
 
97
}
 
98
 
 
99
RunApplet::~RunApplet()
 
100
{
 
101
    KConfig *c = config();
 
102
    c->setGroup("General");
 
103
 
 
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() );
 
110
    c->sync();
 
111
 
 
112
    delete _filterData;
 
113
    KGlobal::locale()->removeCatalogue("krunapplet");
 
114
}
 
115
 
 
116
 
 
117
bool RunApplet::eventFilter( QObject *o, QEvent * e)
 
118
{
 
119
    if ( e->type() == QEvent::MouseButtonRelease ) {
 
120
        emit requestFocus();
 
121
    }
 
122
    return KPanelApplet::eventFilter( o, e );
 
123
}
 
124
 
 
125
void RunApplet::resizeEvent(QResizeEvent*)
 
126
{
 
127
    if(orientation() == Horizontal)
 
128
        {
 
129
            _btn->hide();
 
130
            _input->reparent(this, QPoint(0,0), true);
 
131
            _label->setGeometry(0,0, width(), _label->height());
 
132
 
 
133
            if(height() >= _input->sizeHint().height() + _label->height())
 
134
                {
 
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());
 
141
                    _label->show();
 
142
                }
 
143
            else
 
144
                {
 
145
                    _label->hide();
 
146
 
 
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,
 
154
                                        width(), newHeight);
 
155
                }
 
156
        }
 
157
    else
 
158
        {
 
159
            _btn->show();
 
160
            _btn->setFixedSize(width(), 22);
 
161
            _input->reparent( _hbox, QPoint(0, 0), false);
 
162
            _label->hide();
 
163
        }
 
164
    setButtonText();
 
165
}
 
166
 
 
167
void RunApplet::popupDirectionChange(KPanelApplet::Direction)
 
168
{
 
169
    setButtonText();
 
170
}
 
171
 
 
172
void RunApplet::setButtonText()
 
173
{
 
174
    QString t;
 
175
 
 
176
    if (popupDirection() == Left)
 
177
        {
 
178
            if (width() >= 42)
 
179
                t = i18n("< Run");
 
180
            else
 
181
                t = "<";
 
182
        }
 
183
    else
 
184
        {
 
185
            if(width() >= 42)
 
186
                t = i18n("Run >");
 
187
            else
 
188
                t = ">";
 
189
        }
 
190
 
 
191
    _btn->setText(t);
 
192
}
 
193
 
 
194
int RunApplet::widthForHeight(int ) const
 
195
{
 
196
    return 110;
 
197
}
 
198
 
 
199
int RunApplet::heightForWidth(int ) const
 
200
{
 
201
    return 22;
 
202
}
 
203
 
 
204
void RunApplet::popup_combo()
 
205
{
 
206
    QPoint p;
 
207
    if (popupDirection() == Left)
 
208
        p = mapToGlobal(QPoint(-_input->width()-1, 0));
 
209
    else
 
210
        p = mapToGlobal(QPoint(width()+1, 0));
 
211
    _hbox->move(p);
 
212
    _hbox->show();
 
213
    _input->setFocus();
 
214
}
 
215
 
 
216
void RunApplet::run_command(const QString& command)
 
217
{
 
218
    QString exec;
 
219
 
 
220
    kapp->propagateSessionManager();
 
221
 
 
222
    _filterData->setData( _input->currentText().stripWhiteSpace() );
 
223
    QStringList filters;
 
224
    filters << "kurisearchfilter" << "kshorturifilter";
 
225
    KURIFilter::self()->filterURI( *(_filterData), filters );
 
226
 
 
227
    _input->addToHistory(command);
 
228
    _input->clearEdit();
 
229
 
 
230
    QString cmd = (_filterData->uri().isLocalFile() ? _filterData->uri().path():_filterData->uri().url());
 
231
 
 
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."));
 
236
        emit requestFocus();
 
237
        goto hide;
 
238
    }
 
239
    else if (cmd == "logout")
 
240
        {
 
241
            bool shutdown = kapp->requestShutDown();
 
242
            if( !shutdown )
 
243
            {
 
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." ) );
 
249
                emit requestFocus();
 
250
            }
 
251
            goto hide;
 
252
        }
 
253
    else
 
254
        {
 
255
            switch( _filterData->uriType() )
 
256
                {
 
257
                case KURIFilterData::LOCAL_FILE:
 
258
                case KURIFilterData::LOCAL_DIR:
 
259
                case KURIFilterData::NET_PROTOCOL:
 
260
                case KURIFilterData::HELP:
 
261
                    {
 
262
                        (void) new KRun( _filterData->uri() );
 
263
                        goto hide;
 
264
                    }
 
265
                case KURIFilterData::EXECUTABLE:
 
266
                case KURIFilterData::SHELL:
 
267
                    {
 
268
                        exec = cmd;
 
269
                        if( _filterData->hasArgsAndOptions() )
 
270
                          cmd += _filterData->argsAndOptions();
 
271
                        break;
 
272
                    }
 
273
                case KURIFilterData::UNKNOWN:
 
274
                case KURIFilterData::ERROR:
 
275
                default:
 
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() );
 
280
                    emit requestFocus();
 
281
                    goto hide;
 
282
                }
 
283
        }
 
284
    if (KRun::runCommand( cmd, exec, "" ))
 
285
        goto hide;
 
286
    else
 
287
        {
 
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() );
 
291
            emit requestFocus();
 
292
            goto hide;
 
293
        }
 
294
 
 
295
    return;
 
296
 
 
297
 hide:
 
298
    if (orientation() == Vertical)
 
299
        _hbox->hide();
 
300
}