rendered paste body/***************************************************************************
* Copyright (C) 2006 by Stephen Leaf *
* smileaf@smileaf.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <qlayout.h>
#include <klocale.h>
#include <kglobal.h>
#include <kparts/genericfactory.h>
#include <ksimpleconfig.h>
#include <kglobalsettings.h>
#include <kstandarddirs.h>
#include <kurlrequester.h>
#include <klistview.h>
#include <kopenwith.h>
#include <kpropertiesdialog.h>
#include <kio/job.h>
#include <qcheckbox.h>
#include <qdir.h>
#include <qheader.h>
#include "autostart.h"
class desktop : public KListViewItem {
public:
KService * service;
bool bisDesktop;
QString fileName;
desktop( QString service, QListView *parent ): KListViewItem( parent ) {
bisDesktop = false;
fileName = service;
if (service.endsWith(".desktop")) {
this->service = new KService(service);
bisDesktop = true;
}
}
bool isDesktop() { return bisDesktop; }
~desktop() {
delete service;
}
};
typedef KGenericFactory<autostart, QWidget> autostartFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_autostart, autostartFactory("kcmautostart"))
autostart::autostart(QWidget *parent, const char *name, const QStringList&)
: KCModule(parent, name), myAboutData(0)
{
QGridLayout * AutostartConfigLayout = new QGridLayout( this, 1, 1, 11, 6, "AutostartConfigLayout");
btnAdd = new KPushButton( this, "btnAdd" );
AutostartConfigLayout->addWidget( btnAdd, 0, 1 );
listCMD = new KListView( this, "listCMD" );
listCMD->addColumn( i18n( "Name" ) );
listCMD->addColumn( i18n( "Command" ) );
listCMD->setAllColumnsShowFocus( TRUE );
listCMD->setShowSortIndicator( TRUE );
AutostartConfigLayout->addMultiCellWidget( listCMD, 0, 3, 0, 0 );
QSpacerItem * spacer1 = new QSpacerItem( 71, 170, QSizePolicy::Minimum, QSizePolicy::Expanding );
AutostartConfigLayout->addItem( spacer1, 3, 1 );
btnRemove = new KPushButton( this, "btnRemove" );
AutostartConfigLayout->addWidget( btnRemove, 1, 1 );
btnProperties = new QPushButton( this, "btnProperties" );
AutostartConfigLayout->addWidget( btnProperties, 2, 1 );
btnAdd->setText( i18n( "&Add" ) );
btnAdd->setAccel( QKeySequence( i18n( "Alt+A" ) ) );
btnRemove->setText( i18n( "&Remove" ) );
btnRemove->setAccel( QKeySequence( i18n( "Alt+R" ) ) );
btnProperties->setText( i18n( "&Properties" ) );
btnProperties->setAccel( QKeySequence( i18n( "Alt+P" ) ) );
connect( btnAdd, SIGNAL(clicked()), SLOT(addCMD()) );
connect( btnRemove, SIGNAL(clicked()), SLOT(removeCMD()) );
connect( listCMD, SIGNAL(doubleClicked(QListViewItem*)), SLOT(editCMD(QListViewItem*)) );
connect( btnProperties, SIGNAL(clicked()), SLOT(editCMD()) );
listCMD->setFocus();
load();
KAboutData* about = new KAboutData("autostart", I18N_NOOP("KDE Autostart Manager"), "0.4.1",
I18N_NOOP("KDE Autostart Manager Control Panel Module"),
KAboutData::License_GPL,
I18N_NOOP("(c) 2006 Stephen Leaf"), 0, 0);
about->addAuthor("Stephen Leaf", 0, "smileaf@smileaf.org");
setAboutData( about );
};
autostart::~autostart()
{}
void autostart::load()
{
kgs = new KGlobalSettings();
autostartPath = kgs->autostartPath();
kdDebug() << "According to KDE your Autostart location is: " << autostartPath << endl;
QDir *autostartdir = new QDir( autostartPath );
autostartdir->setFilter( QDir::Files);
const QFileInfoList *list = autostartdir->entryInfoList();
QFileInfoListIterator it( *list );
QFileInfo *fi;
while ( (fi = it.current()) != 0 ) {
QString filename = fi->fileName();
desktop * item = new desktop( fi->absFilePath(), listCMD );
if ( ! item->isDesktop() ) {
if ( fi->isSymLink() ) {
QString link = fi->readLink();
item->setText( 0, filename );
item->setText( 1, link );
} else {
item->setText( 0, filename );
item->setText( 1, filename );
}
} else {
item->setText( 0, item->service->name() );
item->setText( 1, item->service->exec() );
}
++it;
}
}
void autostart::addCMD() {
KService::Ptr service = 0L;
KOpenWithDlg owdlg( this );
if (owdlg.exec() != QDialog::Accepted)
return;
service = owdlg.service();
Q_ASSERT(service);
if (!service)
return; // Don't crash if KOpenWith wasn't able to create service.
KURL desktopTemplate;
if ( service->desktopEntryName().isNull() ) {
KSimpleConfig ksc(autostartPath + service->name() + ".desktop");
ksc.setGroup("Desktop Entry");
ksc.writeEntry("Encoding","UTF-8");
ksc.writeEntry("Exec",service->exec());
ksc.writeEntry("Icon","exec");
ksc.writeEntry("Path","");
ksc.writeEntry("Terminal",false);
ksc.writeEntry("Type","Application");
ksc.sync();
desktopTemplate = KURL( autostartPath + service->name() + ".desktop" );
// FIXME: Make it so you can't give focus to the parent before this goes away.
// If the parent closes before this does, a crash is generated.
KPropertiesDialog dlg( desktopTemplate, this, 0, true /*modal*/, false /*no auto-show*/ );
if ( dlg.exec() != QDialog::Accepted )
return;
} else {
desktopTemplate = KURL( locate("apps", service->desktopEntryPath()) );
// FIXME: Make it so you can't give focus to the parent before this goes away.
// If the parent closes before this does, a crash is generated.
KPropertiesDialog dlg( desktopTemplate, KURL(autostartPath), service->name() + ".desktop", this, 0, true /*modal*/, false /*no auto-show*/ );
if ( dlg.exec() != QDialog::Accepted )
return;
}
desktop * item = new desktop( autostartPath + service->name() + ".desktop", listCMD );
item->setText( 0, item->service->name() );
item->setText( 1, item->service->exec() );
emit changed(true);
}
void autostart::removeCMD() {
if (!listCMD->selectedItem()) return;
KIO::del(((desktop *)listCMD->selectedItem())->fileName);
listCMD->takeItem( listCMD->selectedItem() );
kdDebug() << "Deleting file" << endl;
emit changed(true);
}
void autostart::editCMD(QListViewItem* entry) {
if (!entry) return;
KFileItem kfi = KFileItem( KFileItem::Unknown, KFileItem::Unknown, KURL( ((desktop*)entry)->fileName ), true );
if (! editCMD( kfi )) return;
// Remove and recreate
if (((desktop*)entry)->isDesktop()) {
listCMD->takeItem( listCMD->selectedItem() );
desktop * item = new desktop( ((desktop*)entry)->fileName, listCMD );
item->setText( 0, ((desktop*)entry)->service->name() );
item->setText( 1, ((desktop*)entry)->service->exec() );
}
}
bool autostart::editCMD( KFileItem item) {
KPropertiesDialog dlg( &item, this );
if ( dlg.exec() != QDialog::Accepted )
return false;
kdDebug() << "Saving file" << endl;
emit changed(true);
return true;
}
void autostart::editCMD() {
editCMD( listCMD->selectedItem() );
}
void autostart::defaults(){}
void autostart::save()
{}
int autostart::buttons()
{
return KCModule::Apply|KCModule::Help;
}
QString autostart::quickHelp() const
{
return i18n("This module helps you configure which applications start with KDE.");
}