summaryrefslogtreecommitdiffstats
path: root/src/autostart.cpp
blob: 14387abc5e15e9591744111f048ee3d008b658ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/***************************************************************************
 *   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.             *
 ***************************************************************************/

/* Adapted for use in the Trinity Desktop Environment */

#include <tqlayout.h>

#include <tdelocale.h>
#include <tdeglobal.h>
#include <tdeparts/genericfactory.h>
#include <ksimpleconfig.h>
#include <tdeglobalsettings.h>
#include <kstandarddirs.h>
#include <kurlrequester.h>
#include <tdelistview.h>
#include <kopenwith.h>
#include <kpropertiesdialog.h>
#include <tdeio/job.h>
#include <tqdir.h>
#include <tqheader.h>
#include <tdemessagebox.h>
#include "autostart.h"

class CDesktopItem : public TDEListViewItem {

public:
  KService * service;
  bool bisDesktop;
  KURL fileName;
  int iStartOn;
  enum { AutoStart, Shutdown, ENV };

CDesktopItem( TQString service, int startOn, TQListView *parent ): TDEListViewItem( parent ) {
  iStartOn = startOn;
  fileName = KURL(service);
  if (service.endsWith(".desktop")) {
    this->service = new KService(service);
    bisDesktop = true;
  }
}

~CDesktopItem() {
	if(service) {
		delete service;
		service = NULL;
	}
}

bool isDesktop() { return bisDesktop; }

int startOn() { return iStartOn; }

TQString fStartOn() {
  switch (iStartOn) {
    case AutoStart:
      return i18n("Startup");
      break;
    case Shutdown:
      return i18n("Shutdown");
      break;
    case ENV:
      return i18n("ENV");
      break;
    default:
      return "";
      break;
  }
}

void setStartOn(int start) {
  iStartOn = start;
  setText(2, fStartOn() );
  TDEStandardDirs *ksd = new TDEStandardDirs();
  TDEGlobalSettings * kgs = new TDEGlobalSettings();
  TQString path;
  switch (iStartOn) {
    case AutoStart:
      path = kgs->autostartPath()+"/";
      break;
    case Shutdown:
      path = ksd->localtdedir()+"shutdown/";
      break;
    case ENV:
      path = ksd->localtdedir()+"env/";
      break;
  }
  TDEIO::file_move(fileName, KURL( path + fileName.fileName() ));
  fileName = path + fileName.fileName();
}

void updateService() {
  if (bisDesktop) {
  	if (service) {
  		delete service;
  		service = NULL;
  	}
  	service = new KService( fileName.path() );
    setText( 0, service->name() );
    setText( 1, service->exec() );
    setText( 2, fStartOn() );  	
  }
}  

};  //class CDesktopItem

typedef KGenericFactory<CAutostart, TQWidget> autostartFactory;

K_EXPORT_COMPONENT_FACTORY( kcm_autostart, autostartFactory("kcmautostart"))

CAutostart::CAutostart(TQWidget *parent, const char *name, const TQStringList&)
    : TDECModule(parent, name), myAboutData(0)
{
  TQGridLayout * AutostartConfigLayout = new TQGridLayout( this, 1, 1, 11, 6, "AutostartConfigLayout");

  btnAdd = new KPushButton( this, "btnAdd" );
  AutostartConfigLayout->addWidget( btnAdd, 0, 1 );

  listCMD = new TDEListView( this, "listCMD" );
  listCMD->addColumn( i18n( "Name" ) );
  listCMD->addColumn( i18n( "Command" ) );
  listCMD->addColumn( i18n( "Run on" ) );
  listCMD->setAllColumnsShowFocus( TRUE );
  listCMD->setShowSortIndicator( TRUE );

  AutostartConfigLayout->addMultiCellWidget( listCMD, 0, 4, 0, 0 );
  TQSpacerItem * spacer1 = new TQSpacerItem( 71, 170, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  AutostartConfigLayout->addItem( spacer1, 4, 1 );

  btnRemove = new KPushButton( this, "btnRemove" );
  AutostartConfigLayout->addWidget( btnRemove, 1, 1 );

  btnProperties = new TQPushButton( this, "btnProperties" );
  AutostartConfigLayout->addWidget( btnProperties, 2, 1 );

  cmbStartOn = new TQComboBox( this, "cmbStartOn" );
  AutostartConfigLayout->addWidget( cmbStartOn, 3, 1 );

  cmbStartOn->insertItem( i18n("Startup") );
  cmbStartOn->insertItem( i18n("Shutdown") );
  cmbStartOn->insertItem( i18n("ENV") );
  cmbStartOn->setEnabled(false);

  btnAdd->setText( i18n( "&Add" ) );
  btnAdd->setAccel( TQKeySequence( i18n( "Alt+A" ) ) );
  btnRemove->setText( i18n( "&Remove" ) );
  btnRemove->setAccel( TQKeySequence( i18n( "Alt+R" ) ) );
  btnProperties->setText( i18n( "&Properties" ) );
  btnProperties->setAccel( TQKeySequence( i18n( "Alt+P" ) ) );

  connect( btnAdd, TQT_SIGNAL(clicked()), TQT_SLOT(addCMD()) );
  connect( btnRemove, TQT_SIGNAL(clicked()), TQT_SLOT(removeCMD()) );
  connect( listCMD, TQT_SIGNAL(doubleClicked(TQListViewItem*)), TQT_SLOT(editCMD() ));
  connect( btnProperties, TQT_SIGNAL(clicked()), TQT_SLOT(editCMD()) );
  connect( cmbStartOn, TQT_SIGNAL(activated(int)), TQT_SLOT(setStartOn(int)) );
  connect( listCMD, TQT_SIGNAL(selectionChanged(TQListViewItem*)), TQT_SLOT(selectionChanged(TQListViewItem*)) );

  listCMD->setFocus();

  load();

  TDEAboutData* about = new TDEAboutData("autostart", I18N_NOOP("TDE Autostart Manager"), "0.5",
    I18N_NOOP("TDE Autostart Manager Control Panel Module"),
    TDEAboutData::License_GPL,
    I18N_NOOP("(c) 2006 Stephen Leaf"), 0, 0);

  about->addAuthor("Stephen Leaf", 0, "smileaf@smileaf.org");
  setAboutData( about );

};


CAutostart::~CAutostart()
{}

void CAutostart::load()
{
  kgs = new TDEGlobalSettings();
  kdDebug() << "According to TDE your Autostart location is: " << kgs->autostartPath() << endl;
  TDEStandardDirs *ksd = new TDEStandardDirs();

  TQString path;
  for (int x=0;x<3;x++) {
    if (x==0)
      path = kgs->autostartPath();
    else if (x==1)
      path = ksd->localtdedir() + "/shutdown";
    else if (x==2)
      path = ksd->localtdedir() + "/env";

    if (! TDEStandardDirs::exists(path))
      TDEStandardDirs::makeDir(path);

    TQDir *autostartdir = new TQDir( path );
    autostartdir->setFilter( TQDir::Files);
    const TQFileInfoList *list = autostartdir->entryInfoList();
    TQFileInfoListIterator it( *list );
    TQFileInfo *fi;
    while ( (fi = it.current()) != 0 ) {
      TQString filename = fi->fileName();
      CDesktopItem * item = new CDesktopItem( fi->absFilePath(), x, listCMD );
      if ( ! item->isDesktop() ) {
        if ( fi->isSymLink() ) {
          TQString link = fi->readLink();
          item->setText( 0, filename );
          item->setText( 1, link );
          item->setText( 2, item->fStartOn() );
        }
        else {
          item->setText( 0, filename );
          item->setText( 1, filename );
          item->setText( 2, item->fStartOn() );
        }
      }
      else {
        item->setText( 0, item->service->name() );
        item->setText( 1, item->service->exec() );
        item->setText( 2, item->fStartOn() );
      }
      ++it;
    }
  }
}

void CAutostart::addCMD() {
  KService::Ptr service = 0L;
  KOpenWithDlg owdlg( this );
  if (owdlg.exec() != TQDialog::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->type() == "Service") {
    KMessageBox::error(0, i18n("Cannot add a Service entry to the list of autostart modules.\n") + service->desktopEntryPath());
		return;
	}
  else if ( service->desktopEntryName().isNull() ) {
    desktopTemplate = KURL( kgs->autostartPath() + service->name() + ".desktop" );
    KSimpleConfig ksc(desktopTemplate.path());
    ksc.setGroup("Desktop Entry");
    ksc.writeEntry("Encoding","UTF-8");
    ksc.writeEntry("Exec",service->exec());
    ksc.writeEntry("Icon","application-x-executable");
    ksc.writeEntry("Path","");
    ksc.writeEntry("Terminal",false);
    ksc.writeEntry("Type","Application");
    ksc.sync();

    KPropertiesDialog *dlg = new KPropertiesDialog( desktopTemplate, this, 0, true /*modal*/, false /*no auto-show*/ );
    if ( dlg->exec() != TQDialog::Accepted )
      return;
  }
  else {
    desktopTemplate = KURL( locate("apps", service->desktopEntryPath()) );
		// Make sure the URL is not empty to avoid crashing the application
		if (desktopTemplate.isEmpty()) {
      KMessageBox::error(0, i18n("Can't find a matching entry for the selected item.\n") + service->desktopEntryPath());
			return;
		}
		
    KPropertiesDialog *dlg = new KPropertiesDialog( desktopTemplate, KURL(kgs->autostartPath()),
      service->name() + ".desktop", this, 0, true /*modal*/, false /*no auto-show*/ );
    if ( dlg->exec() != TQDialog::Accepted )
      return;
  }

  CDesktopItem * item = new CDesktopItem( kgs->autostartPath() + service->name() + ".desktop", CDesktopItem::AutoStart, listCMD );
  item->setText( 0, item->service->name() );
  item->setText( 1, item->service->exec() );
  item->setText( 2, item->fStartOn() );
  emit changed(true);
}

void CAutostart::removeCMD() {
  if (!listCMD->selectedItem())
    return;

	TQListViewItem *currItem = listCMD->selectedItem();
  TDEIO::del(((CDesktopItem *)currItem)->fileName);
  listCMD->takeItem(currItem);
  delete currItem;
  
  kdDebug() << "Deleting file" << endl;
  emit changed(true);
}

void CAutostart::editCMD() {
  CDesktopItem *currItem = (CDesktopItem*)(listCMD->selectedItem());
  if (!currItem) return;
  	
  KFileItem kfi = KFileItem(KFileItem::Unknown, KFileItem::Unknown, KURL(currItem->fileName), true);
  if (!editCMD(kfi)) return;

	currItem->updateService();
}

bool CAutostart::editCMD( KFileItem item) {
  KPropertiesDialog *dlg = new KPropertiesDialog(&item, this);
  if ( dlg->exec() != TQDialog::Accepted )
    return false;
  
  kdDebug() << "Saving file" << endl;
  emit changed(true);
  return true;
}

void CAutostart::setStartOn( int index ) {
  ((CDesktopItem*)listCMD->currentItem())->setStartOn(index);
}

void CAutostart::selectionChanged(TQListViewItem* entry) {
  cmbStartOn->setEnabled( (entry != 0) );
  cmbStartOn->setCurrentItem( ((CDesktopItem*)entry)->startOn() );
}

void CAutostart::defaults(){}

void CAutostart::save(){}

int CAutostart::buttons()
{
  return TDECModule::Apply|TDECModule::Help;
}

TQString CAutostart::quickHelp() const
{
  return i18n("This module helps configure which applications TDE runs when starting and exiting.");
}