summaryrefslogtreecommitdiffstats
path: root/src/tdmtheme.cpp
blob: 8451810913897d3d8734a33949d7f6e30e193222 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/***************************************************************************
 *   Copyright (C) 2005-2007 by Stephen Leaf <smileaf@gmail.com>           *
 *   Copyright (C) 2006 by Oswald Buddenhagen <ossi@kde.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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "tdmtheme.h"

#include <kdialog.h>
#include <tdeglobal.h>
#include <tdeio/job.h>
#include <tdeio/netaccess.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kprogress.h>
#include <kstandarddirs.h>
#include <ktar.h>
#include <kurlrequester.h>
#include <kurlrequesterdlg.h>
#include <tdeparts/genericfactory.h>

#include <tqcheckbox.h>
#include <tqdir.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqptrlist.h>
#include <tqpixmap.h>
#include <tqpushbutton.h>
#include <tqlistview.h>
#include <tqwhatsthis.h>
#include <tqwidget.h>

#include <unistd.h>

class ThemeData : public TQListViewItem {
  public:
	ThemeData( TQListView *parent = 0 ) : TQListViewItem( parent ) {}

	TQString name;
	TQString path;
	TQString screenShot;
	TQString copyright;
	TQString description;
};

typedef KGenericFactory<TDMThemeWidget, TQWidget> tdmthemeFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_tdmtheme, tdmthemeFactory("kcmtdmtheme"))

TDMThemeWidget::TDMThemeWidget( TQWidget *parent, const char *name, const TQStringList& )
	: TDECModule(parent, name), config( 0L )
{
	TQGridLayout *ml = new TQGridLayout( this );
	ml->setSpacing( KDialog::spacingHint() );
	ml->setMargin( KDialog::marginHint() );

	sakWarning = new TQLabel( this );
	sakWarning->setText( i18n( "Theming is not available due to enabled Secure Attention Key (SAK) support" ) );
	ml->addMultiCellWidget( sakWarning, 0, 0, 0, 2 );
	sakWarning->hide();

	cUseTheme = new TQCheckBox( this );
	cUseTheme->setText( i18n( "En&able TDM Themes" ) );
	ml->addMultiCellWidget( cUseTheme, 0, 0, 0, 2 );

	themeWidget = new TQListView( this );
	themeWidget->addColumn( i18n( "Theme" ) );
	themeWidget->addColumn( i18n( "Author" ) );
	themeWidget->setAllColumnsShowFocus( true );
	themeWidget->setShowSortIndicator( true );
	themeWidget->setRootIsDecorated( false );
	TQWhatsThis::add( themeWidget, i18n("This is a list of installed themes.\n"
	                                "Click the one to be used.") );

	ml->addMultiCellWidget( themeWidget, 1, 3, 0, 1 );

	preview = new TQLabel( this );
	preview->setFixedSize( TQSize( 200, 150 ) );
	preview->setScaledContents( true );
	TQWhatsThis::add(preview, i18n("This is a screen shot of what TDM will look like.") );

	ml->addWidget( preview, 1, 2 );

	info = new TQLabel( this );
	info->setMaximumWidth( 200 );
	info->setAlignment( int( TQLabel::AlignTop | TQLabel::WordBreak) );
	TQWhatsThis::add(info, i18n("This contains information about the selected theme.") );

	ml->addMultiCellWidget( info, 3, 4, 2, 2 );

	bInstallTheme = new TQPushButton( i18n("Install &new theme"), this );
	TQWhatsThis::add(bInstallTheme, i18n("This will install a theme into the theme directory.") );

	ml->addWidget( bInstallTheme, 4, 0 );

	bRemoveTheme = new TQPushButton( i18n("&Remove theme"), this );
	TQWhatsThis::add(bRemoveTheme, i18n("This will remove the selected theme.") );

	ml->addWidget( bRemoveTheme, 4, 1 );

	connect( themeWidget, TQT_SIGNAL(selectionChanged()), TQT_SLOT(themeSelected()) );
	connect( bInstallTheme, TQT_SIGNAL(clicked()), TQT_SLOT(installNewTheme()) );
	connect( bRemoveTheme, TQT_SIGNAL(clicked()), TQT_SLOT(removeSelectedThemes()) );
	connect( cUseTheme, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( toggleUseTheme( bool ) ) );

	TQStringList themeDirs = TDEGlobal::dirs()->findDirs("data","tdm/");
	themeDir = TDEGlobal::dirs()->findDirs("data","tdm/").last();
	TQDir d;
	for ( TQStringList::Iterator dirs = themeDirs.begin(); dirs != themeDirs.end(); ++dirs ) {
		kdDebug() << "Loading themes... ( " + *dirs + "themes/" + " )" << endl;
		d.setPath( *dirs + "themes/" );
		d.setFilter( TQDir::Dirs );
		TQStringList list = d.entryList();
		for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
			if (*it == "." || *it == "..") continue;
			insertTheme( *dirs + "themes/" + *it );
		}
	}

	load();

	if (getuid() != 0) {
		cUseTheme->setEnabled( false );
		setReadOnly(false);
	}
}

void TDMThemeWidget::selectTheme( const TQString &path )
{
	for ( TQListViewItemIterator tdi( themeWidget ); tdi.current(); tdi++ ) {
		ThemeData * td = ((ThemeData *)*tdi);
		if (td->path == path) {
			themeWidget->clearSelection();
			themeWidget->setSelected(td, true );
			updateInfoView( td );
		}
	}
}

void TDMThemeWidget::load()
{
	TQString tdmrc = TDEGlobal::dirs()->findResource("config", "tdm/tdmdistrc");
	if ( tdmrc.isEmpty() ) {
		tdmrc = TDEGlobal::dirs()->findResource("config", "tdm/tdmrc");
	}
	if ( tdmrc.isEmpty() ) {
		kdError() << "Failed to find tdm resource file tdmrc!" << endl;

		KMessageBox msgBox;
		msgBox.sorry(0, i18n("I can't find the configuration file for the Login Manager, tdm. Check you have set up the Login Manager from the Control Center, and that you have permision to read and write the Login Manager's configuration file.\n\n You must set-up the Login Manager before you can use its themes."), i18n("Can't find Configuration File"));

		delete config;
		config = NULL;
	} else {
		kdDebug() << "Loading... ( " + tdmrc + " )" << endl;

		delete config;
		config = new TDEConfig( tdmrc );

		config->setGroup( "X-*-Greeter" );
		bool sakEnabled = config->readBoolEntry( "UseSAK", true );
		if (sakEnabled) {
			// FIXME
			// Integrate KDE_BINDIR with Autotools and use this line instead of the one below it...
			// if (system(KDE_BINDIR "/tsak checkdeps") != 0) {
			if (system("tsak checkdeps") != 0) {
				sakEnabled = false;
			}
		}
		if (sakEnabled) {
			cUseTheme->hide();
			sakWarning->show();
			cUseTheme->setEnabled(false);
			setReadOnly(true);
		}
		else {
			sakWarning->hide();
			cUseTheme->show();
			cUseTheme->setEnabled(true);
			setReadOnly(false);
		}

		config->setGroup( "X-*-Greeter" );
		cUseTheme->setChecked( config->readBoolEntry( "UseTheme", false ) );

		selectTheme( config->readEntry( "Theme", themeDir + "circles" ) );
	}
}

void TDMThemeWidget::save()
{
	kdDebug() << "Saving: Theme " << defaultTheme->path << " - UseTheme " << cUseTheme->isChecked() << endl;
	config->writeEntry( "Theme", defaultTheme->path );
	config->writeEntry( "UseTheme", cUseTheme->isChecked() );
	config->sync();
}

void TDMThemeWidget::defaults()
{
	selectTheme( themeDir + "circles" );

	emit changed(true);
}

void TDMThemeWidget::toggleUseTheme(bool useTheme) {
	setReadOnly( useTheme );
	emit changed(true);
}

void TDMThemeWidget::setReadOnly(bool ro)
{
	if (!cUseTheme->isEnabled()) {
		ro = true;
	}
	themeWidget->setEnabled( ro );
	bInstallTheme->setEnabled( ro );
	bRemoveTheme->setEnabled( ro );
}

void TDMThemeWidget::insertTheme( const TQString &_theme )
{
	TDEConfig * themeConfig;
	TQString name;

	kdDebug() << "Looking for " << _theme << "/KdmGreeterTheme.desktop" << endl;
	themeConfig = new TDEConfig( _theme + "/KdmGreeterTheme.desktop");
	themeConfig->setGroup( "GdmGreeterTheme" );
	name = themeConfig->readEntry( "Name" );
	if (name.isEmpty()) {
		kdDebug() << "Looking for " << _theme << "/GdmGreeterTheme.desktop" << endl;
		themeConfig = new TDEConfig( _theme + "/GdmGreeterTheme.desktop");
		themeConfig->setGroup( "GdmGreeterTheme" );

		name = themeConfig->readEntry( "Name" );
		if (name.isEmpty())
			return;
	}

	for ( TQListViewItemIterator tdi( themeWidget ); tdi.current(); tdi++ ) {
		ThemeData * td = ((ThemeData *)*tdi);
		if (td->name == name)
			return;
	}
	kdDebug() << "Adding theme " << name << endl;

	ThemeData *child = new ThemeData( themeWidget );
	child->setText( 0, name );
	child->setText( 1, themeConfig->readEntry( "Author" ) );
	child->name = name;
	child->path = _theme;
	child->screenShot = themeConfig->readEntry( "Screenshot" );
	child->copyright = themeConfig->readEntry( "Copyright" );
	child->description = themeConfig->readEntry( "Description" );
}

void TDMThemeWidget::updateInfoView( ThemeData *theme )
{
	info->setText(
		((theme->copyright.length() > 0) ?
			i18n("<qt><strong>Copyright:</strong> %1<br/></qt>",
				theme->copyright) : "") +
		((theme->description.length() > 0) ?
			i18n("<qt><strong>Description:</strong> %1</qt>",
				theme->description) : "") );
	preview->setPixmap( theme->path + '/' + theme->screenShot );
	preview->setText( theme->screenShot.isEmpty() ?
		"Screenshot not available" : TQString() );
}

void TDMThemeWidget::installNewTheme()
{
	KURLRequesterDlg fileRequester( TQString(), this, i18n("Drag or Type Theme URL") );
	fileRequester.urlRequester()->setMode( KFile::File | KFile::Directory | KFile::ExistingOnly );
	KURL themeURL = fileRequester.getURL();
	if (themeURL.isEmpty())
		return;

	TQString themeTmpFile;

	if (!TDEIO::NetAccess::download( themeURL, themeTmpFile, this )) {
		TQString sorryText;
		if (themeURL.isLocalFile())
			sorryText = i18n("Unable to find the TDM theme archive %1.",themeURL.prettyURL());
		else
			sorryText = i18n("Unable to download the TDM theme archive;\n"
			                 "please check that address %1 is correct.",themeURL.prettyURL());
		KMessageBox::sorry( this, sorryText );
		return;
	}

	TQList<KArchiveDirectory> foundThemes;

	KTar archive( themeTmpFile );
	archive.open( IO_ReadOnly );

	const KArchiveDirectory *archDir = archive.directory();
	TQStringList entries = archDir->entries();
	for (TQStringList::Iterator ent = entries.begin(); ent != entries.end(); ++ent) {
		const KArchiveEntry *possibleDir = archDir->entry( *ent );
		if (possibleDir->isDirectory()) {
			const KArchiveDirectory *subDir =
				static_cast<const KArchiveDirectory *>( possibleDir );
			if (subDir->entry( "KdmGreeterTheme.desktop" ))
				foundThemes.append( subDir );
			else if (subDir->entry( "GdmGreeterTheme.desktop" ))
				foundThemes.append( subDir );
		}
	}

	if (foundThemes.isEmpty())
		KMessageBox::error( this, i18n("The file is not a valid TDM theme archive.") );
	else {
		KProgressDialog progressDiag( this,
			i18n("Installing TDM themes"), TQString() );
		progressDiag.setModal( true );
		progressDiag.setAutoClose( true );
		progressDiag.progressBar()->setTotalSteps( foundThemes.count() );
		progressDiag.show();

		for ( KArchiveDirectory * ard = foundThemes.first(); foundThemes.current(); foundThemes.next() ) {
			progressDiag.setLabel(
				i18n("<qt>Installing <strong>%1</strong> theme</qt>", ard->name() ) );

			TQString path = themeDir + "themes/" + ard->name();
			kdDebug() << "Unpacking new theme to " << path << endl;
			ard->copyTo( path, true );
			if (TQDir( path ).exists())
				insertTheme( path );

			progressDiag.progressBar()->setValue( progressDiag.progressBar()->value() + 1 );
			if (progressDiag.wasCancelled())
				break;
		}
		emit changed(true);
	}

	archive.close();

	TDEIO::NetAccess::removeTempFile( themeTmpFile );
}

void TDMThemeWidget::themeSelected()
{
	if (themeWidget->selectedItem()) {
		defaultTheme = (ThemeData *)(themeWidget->selectedItem());
		updateInfoView( defaultTheme );
	} else
		updateInfoView( 0 );
	emit changed(true);
}

void TDMThemeWidget::removeSelectedThemes()
{
	TQListViewItem * themes = themeWidget->selectedItem();
	if (!themes)
		return;
	if (KMessageBox::questionYesNoList( this,
			i18n("Are you sure you want to remove this TDM theme?"),
			themes->text(0), i18n("Remove theme?") ) != KMessageBox::Yes)
		return;
	TDEIO::del( ((ThemeData *)themes)->path ); // XXX error check

	themeWidget->takeItem( themes );
}

#include "tdmtheme.moc"