1
/*****************************************************************
3
Copyright (c) 1996-2001 the kicker authors. See file AUTHORS.
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
******************************************************************/
33
#include <kapplication.h>
36
#include <kiconloader.h>
37
#include <kio/global.h>
42
#include <kdesktopfile.h>
43
#include <kstandarddirs.h>
44
#include <ktoolinvocation.h>
45
#include <kworkspace.h>
46
#include "konsole_mnu.h"
50
KDE_EXPORT void* init_kickermenu_konsole()
52
KGlobal::locale()->insertCatalog("libkickermenu_konsole");
53
return new KonsoleMenuFactory;
57
KonsoleMenu::KonsoleMenu(QWidget *parent)
58
: KPanelMenu("", parent),
60
m_bookmarksSession(0),
61
m_bookmarkHandlerSession(0)
65
KonsoleMenu::~KonsoleMenu()
67
KGlobal::locale()->removeCatalog("libkickermenu_konsole");
70
static void insertItemSorted(KMenu *menu,
72
const QString &txt, int id)
74
const int defaultId = 1; // The id of the 'new' item.
75
int index = menu->indexOf(defaultId);
76
int count = menu->actions().count();
79
index++; // Skip separator
85
index = -1; // Insert at end
88
if (menu->text(menu->idAt(index)) > txt)
89
break; // Insert before this item
92
menu->insertItem(iconSet, txt, id, index);
96
void KonsoleMenu::initialize()
103
setInitialized(true);
105
QStringList list = KGlobal::dirs()->findAllResources("data",
107
KStandardDirs::NoDuplicates);
109
QString defaultShell = KStandardDirs::locate("data", "konsole/shell.desktop");
110
list.prepend(defaultShell);
115
for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
117
if ((*it == defaultShell) && (id != 1))
122
KDesktopFile conf(*it);
123
const KConfigGroup cg = conf.desktopGroup();
124
QString text = cg.readEntry("Name");
126
// try to locate the binary
127
QString exec= cg.readPathEntry("Exec");
128
if (exec.startsWith("su -c \'"))
130
exec = exec.mid(7,exec.length()-8);
133
exec = KRun::binaryName(exec, false);
134
exec = KShell::tildeExpand(exec);
135
QString pexec = KGlobal::dirs()->findExe(exec);
136
if (text.isEmpty() ||
137
cg.readEntry("Type") != "KonsoleApplication" ||
138
(!exec.isEmpty() && pexec.isEmpty()))
142
insertItemSorted(this, KIcon(cg.readEntry("Icon", "konsole")),
145
sessionList.append(fi.completeBaseName());
153
m_bookmarkHandlerSession = new KonsoleBookmarkHandler(this, false);
154
m_bookmarksSession = m_bookmarkHandlerSession->menu();
156
insertItem(KIcon("keditbookmarks"),
157
i18n("New Session at Bookmark"), m_bookmarksSession);
158
connect(m_bookmarkHandlerSession,
159
SIGNAL(openUrl(const QString&, const QString&)),
160
SLOT(newSession(const QString&, const QString&)));
164
QByteArray screenDir = getenv("SCREENDIR");
166
if (screenDir.isEmpty())
168
screenDir = QFile::encodeName(QDir::homePath()) + "/.screen/";
171
QStringList sessions;
172
// Can't use QDir as it doesn't support FIFOs :(
173
DIR *dir = opendir(screenDir);
176
struct dirent *entry;
177
while ((entry = readdir(dir)))
179
QByteArray path = screenDir + '/' + QByteArray(entry->d_name);
181
if (stat(path, &st) != 0)
187
if (S_ISFIFO(st.st_mode) && !(st.st_mode & 0111) && // xbit == attached
188
(fd = open(path, O_WRONLY | O_NONBLOCK)) != -1)
191
screenList.append(QFile::decodeName(entry->d_name));
192
insertItem(KIcon("konsole"),
193
i18nc("Screen is a program for controlling screens",
194
"Screen at %1", entry->d_name), id);
201
// reset id as we are now going to populate the profiles submenu
204
delete m_profileMenu;
205
m_profileMenu = new KMenu(this);
206
QStringList profiles = KGlobal::dirs()->findAllResources("data",
207
"konsole/profiles/*",
208
KStandardDirs::NoDuplicates);
209
m_profiles.resize(profiles.count());
210
QStringList::ConstIterator pEnd = profiles.end();
211
for (QStringList::ConstIterator pIt = profiles.begin(); pIt != pEnd; ++pIt)
213
QFileInfo info(*pIt);
214
QString profileName = KIO::decodeFileName(info.baseName());
215
QString niceName = profileName;
216
KConfig cfg(*pIt, KConfig::OnlyLocal);
217
if (cfg.hasGroup("Profile"))
219
const KConfigGroup cg( &cfg, "Profile" );
220
if (cg.hasKey("Name"))
222
niceName = cg.readEntry("Name");
226
m_profiles[id] = profileName;
228
m_profileMenu->insertItem(niceName, id);
231
int profileID = insertItem(i18n("New Session Using Profile"),
235
// we don't have any profiles, disable the menu
236
setItemEnabled(profileID, false);
238
connect(m_profileMenu, SIGNAL(activated(int)), SLOT(launchProfile(int)));
241
insertItem(KIcon("view-refresh"),
242
i18n("Reload Sessions"), this, SLOT(reinitialize()));
245
void KonsoleMenu::slotExec(int id)
253
KWorkSpace::propagateSessionManager();
255
if (id < sessionList.count())
258
args << sessionList[id];
265
args << screenList[id - sessionList.count()];
267
KToolInvocation::kdeinitExec("konsole", args);
271
void KonsoleMenu::launchProfile(int id)
273
if (id < 1 || id > m_profiles.count())
279
// this is a session, not a bookmark, so execute that instead
281
args << "--profile" << m_profiles[id];
282
KToolInvocation::kdeinitExec("konsole", args);
285
KUrl KonsoleMenu::baseURL() const
288
/*url.setPath(se->getCwd()+'/');*/
292
void KonsoleMenu::newSession(const QString& sURL, const QString& title)
296
KUrl url = KUrl(sURL);
297
if ((url.protocol() == "file") && (url.hasPath()))
299
args << "-T" << title;
300
args << "--workdir" << url.path();
301
KToolInvocation::kdeinitExec("konsole", args);
304
else if ((!url.protocol().isEmpty()) && (url.hasHost()))
306
QString protocol = url.protocol();
307
QString host = url.host();
308
args << "-T" << title;
309
args << "-e" << protocol.toLatin1(); /* argv[0] == command to run. */
311
args << "-l" << url.user().toLatin1();
313
args << host.toLatin1();
314
KToolInvocation::kdeinitExec("konsole", args);
318
* We can't create a session without a protocol.
319
* We should ideally popup a warning, about an invalid bookmark.
324
//*****************************************************************
326
KonsoleMenuFactory::KonsoleMenuFactory(QObject *parent, const char *name)
327
: KLibFactory(parent)
330
KIconLoader::global()->addAppDir("konsole");
331
KGlobal::locale()->insertCatalog("konsolemenuapplet");
334
QObject* KonsoleMenuFactory::createObject(QObject *parent, const char*, const QStringList&)
336
return new KonsoleMenu((QWidget*)parent);
339
#include "konsole_mnu.moc"