summaryrefslogtreecommitdiffstats
path: root/kcontrol/iccconfig/iccconfig.cpp
blob: 41ddfe8e323285069046dbb0b2b15e7e3f450633 (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
 * iccconfig.cpp
 *
 * Copyright (c) 2009-2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>
 *
 *  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 <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>

#include <dcopclient.h>

#include <kaboutdata.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kglobal.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kinputdialog.h>
#include <kurlrequester.h>
#include <kgenericfactory.h>

#include <unistd.h>
#include <ksimpleconfig.h>
#include <string>
#include <stdio.h>
#include <tqstring.h>

#include "iccconfig.h"

using namespace std;

/**** DLL Interface ****/
typedef KGenericFactory<KICCConfig, TQWidget> KICCCFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_iccconfig, KICCCFactory("kcmiccconfig") )

KSimpleConfig *config;
KSimpleConfig *systemconfig;

/**** KICCConfig ****/

KICCConfig::KICCConfig(TQWidget *parent, const char *name, const TQStringList &)
  : KCModule(KICCCFactory::instance(), parent, name)
{

  TQVBoxLayout *layout = new TQVBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint());
  config = new KSimpleConfig( TQString::fromLatin1( "kiccconfigrc" ));
  systemconfig = new KSimpleConfig( TQString::fromLatin1( KDE_CONFDIR "/kicc/kiccconfigrc" ));

  KAboutData *about =
  new KAboutData(I18N_NOOP("kcmiccconfig"), I18N_NOOP("KDE ICC Profile Control Module"),
                0, 0, KAboutData::License_GPL,
                I18N_NOOP("(c) 2009,2010 Timothy Pearson"));

  about->addAuthor("Timothy Pearson", 0, "kb9vqf@pearsoncomputing.net");
  setAboutData( about );

  base = new ICCConfigBase(this);
  layout->add(base);

  setRootOnlyMsg(i18n("<b>The global ICC color profile is a system wide setting, and requires administrator access</b><br>To alter the system's global ICC profile, click on the \"Administrator Mode\" button below."));
  setUseRootOnlyMsg(true);

  connect(base->systemEnableSupport, TQT_SIGNAL(clicked()), TQT_SLOT(changed()));
  connect(base->systemEnableSupport, TQT_SIGNAL(toggled(bool)), base->systemIccFile, TQT_SLOT(setEnabled(bool)));
  connect(base->enableSupport, TQT_SIGNAL(clicked()), TQT_SLOT(changed()));
  connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->iccFile, TQT_SLOT(setEnabled(bool)));
  connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->randrScreenList, TQT_SLOT(setEnabled(bool)));
  connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->iccProfileList, TQT_SLOT(setEnabled(bool)));
  connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->addProfileButton, TQT_SLOT(setEnabled(bool)));
  connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->renameProfileButton, TQT_SLOT(setEnabled(bool)));
  connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->deleteProfileButton, TQT_SLOT(setEnabled(bool)));
  connect(base->iccProfileList, TQT_SIGNAL(activated(int)), this, TQT_SLOT(selectProfile(int)));
  connect(base->randrScreenList, TQT_SIGNAL(activated(int)), this, TQT_SLOT(selectScreen(int)));
  connect(base->iccFile, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(updateArray()));
  connect(base->systemIccFile, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(changed()));

  connect(base->addProfileButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addProfile()));
  connect(base->renameProfileButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(renameProfile()));
  connect(base->deleteProfileButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteProfile()));

  load();

  if (!config->checkConfigFilesWritable( true )) {
    base->enableSupport->setEnabled(false);
    base->randrScreenList->setEnabled(false);
    base->iccProfileList->setEnabled(false);
    base->iccFile->setEnabled(false);
    base->addProfileButton->setEnabled(false);
    base->renameProfileButton->setEnabled(false);
    base->deleteProfileButton->setEnabled(false);
  }

  if (getuid() != 0 || !systemconfig->checkConfigFilesWritable( true )) {
    base->systemEnableSupport->setEnabled(false);
    base->systemIccFile->setEnabled(false);
  }
}

KICCConfig::~KICCConfig()
{
    delete [] iccFileArray;
    delete config;
    delete systemconfig;
}

void KICCConfig::deleteProfile () {
	int i;
	TQString *iccFileArrayNew;

	// Delete the profile
	config->deleteGroup(base->iccProfileList->currentText());
	base->iccProfileList->removeItem(base->iccProfileList->currentItem());
	base->iccProfileList->setCurrentItem(base->iccProfileList->count()-1);

	// Contract the profile memory
	numberOfProfiles--;
	iccFileArrayNew = new QString[numberOfProfiles*numberOfScreens];
	for (i=0;i<(numberOfProfiles*numberOfScreens);i++) {
		iccFileArrayNew[i] = iccFileArray[i];
	}
	delete [] iccFileArray;
	iccFileArray = iccFileArrayNew;
}

void KICCConfig::renameProfile () {
	int i;
	TQString *iccFileArrayNew;

	// Pop up a text entry box asking for the name of the new profile
	bool _ok = false;
	bool _end = false;
	TQString _new;
	TQString _text = i18n("Please enter the new profile name below:");
	TQString _error;

	while (!_end) {
		_new = KInputDialog::getText( i18n("ICC Profile Configuration"),  _error + _text, TQString::null, &_ok, this);
		if (!_ok ) {
			_end = true;
		} else {
			_error = TQString();
			if (!_new.isEmpty()) {
				if (findProfileIndex(_new) != -1)
					_error = i18n("Error: A profile with that name already exists") + TQString("\n");
				else
					_end = true;
			}
		}
	}

	// Rename the profile
	config->deleteGroup(base->iccProfileList->currentText());
	base->iccProfileList->changeItem(_new, base->iccProfileList->currentItem());

	updateDisplayedInformation();
	emit changed();
}

void KICCConfig::addProfile () {
	int i;
	TQString *iccFileArrayNew;

	// Pop up a text entry box asking for the name of the new profile
	bool _ok = false;
	bool _end = false;
	TQString _new;
	TQString _text = i18n("Please enter the new profile name below:");
	TQString _error;

	while (!_end) {
		_new = KInputDialog::getText( i18n("ICC Profile Configuration"),  _error + _text, TQString::null, &_ok, this);
		if (!_ok ) {
			_end = true;
		} else {
			_error = TQString();
			if (!_new.isEmpty()) {
				if (findProfileIndex(_new) != -1)
					_error = i18n("Error: A profile with that name already exists") + TQString("\n");
				else
					_end = true;
			}
		}
	}

	// Expand the profile memory
	numberOfProfiles++;
	iccFileArrayNew = new QString[numberOfProfiles*numberOfScreens];
	for (i=0;i<((numberOfProfiles-1)*numberOfScreens);i++) {
		iccFileArrayNew[i] = iccFileArray[i];
	}
	delete [] iccFileArray;
	iccFileArray = iccFileArrayNew;
	for (;i<(numberOfProfiles*numberOfScreens);i++) {
		iccFileArray[i] = "";
	}

	// Insert the new profile name
	base->iccProfileList->insertItem(_new, -1);
	base->iccProfileList->setCurrentItem(base->iccProfileList->count()-1);

	updateDisplayedInformation();
	emit changed();
}

void KICCConfig::load()
{
	load( false );
}

void KICCConfig::selectProfile (int slotNumber) {
	updateDisplayedInformation();
	emit changed();
}

void KICCConfig::selectScreen (int slotNumber) {
	updateDisplayedInformation();
}

void KICCConfig::updateArray (void) {
	iccFileArray[((base->iccProfileList->currentItem())*(base->randrScreenList->count()))+(base->randrScreenList->currentItem())] = base->iccFile->url();
	config->setGroup(base->iccProfileList->currentText());
	if (config->readEntry(base->randrScreenList->currentText()) != iccFileArray[((base->iccProfileList->currentItem())*(base->randrScreenList->count()))+(base->randrScreenList->currentItem())]) {
		emit changed();
	}
}

void KICCConfig::updateDisplayedInformation () {
	base->iccFile->setURL(iccFileArray[((base->iccProfileList->currentItem())*(base->randrScreenList->count()))+(base->randrScreenList->currentItem())]);
}

TQString KICCConfig::extractFileName(TQString displayName, TQString profileName) {
	//
}

int KICCConfig::findProfileIndex(TQString profileName) {
	int i;
	for (i=0;i<numberOfProfiles;i++) {
		if (base->iccProfileList->text(i) == profileName) {
			return i;
		}
	}
	return -1;
}

int KICCConfig::findScreenIndex(TQString screenName) {
	int i;
	for (i=0;i<(base->randrScreenList->count());i++) {
		if (base->randrScreenList->text(i) == screenName) {
			return i;
		}
	}
	return -1;
}

void KICCConfig::load(bool useDefaults )
{
  //Update the toggle buttons with the current configuration
  int i;
  int j;

  // FIXME Should use font size (basically resultant string length) to set button widths...
  base->addProfileButton->setFixedWidth(110);
  base->renameProfileButton->setFixedWidth(90);
  base->deleteProfileButton->setFixedWidth(90);

  XRROutputInfo *output_info;
  KRandrSimpleAPI *randrsimple = new KRandrSimpleAPI::KRandrSimpleAPI();

  config->setReadDefaults( useDefaults );

  config->setGroup(NULL);
  base->enableSupport->setChecked(config->readBoolEntry("EnableICC", false));
  base->randrScreenList->setEnabled(config->readBoolEntry("EnableICC", false));
  base->iccProfileList->setEnabled(config->readBoolEntry("EnableICC", false));
  base->iccFile->setEnabled(config->readBoolEntry("EnableICC", false));
  base->addProfileButton->setEnabled(config->readBoolEntry("EnableICC", false));
  base->renameProfileButton->setEnabled(config->readBoolEntry("EnableICC", false));
  base->deleteProfileButton->setEnabled(config->readBoolEntry("EnableICC", false));

  numberOfScreens = 0;
  if (randrsimple->isValid() == true) {
      randr_display = XOpenDisplay(NULL);
      randr_screen_info = randrsimple->read_screen_info(randr_display);
      for (i = 0; i < randr_screen_info->n_output; i++) {
          output_info = randr_screen_info->outputs[i]->info;
          base->randrScreenList->insertItem(output_info->name, -1);
          numberOfScreens++;
      }
  }
  else {
      base->randrScreenList->insertItem("Default", -1);
      numberOfScreens++;
  }

  // Find all profile names
  numberOfProfiles = 0;
  cfgProfiles = config->groupList();
  for (TQStringList::Iterator i(cfgProfiles.begin()); i != cfgProfiles.end(); ++i) {
      base->iccProfileList->insertItem((*i), -1);
      numberOfProfiles++;
  }
  if (numberOfProfiles == 0) {
      base->iccProfileList->insertItem("<default>", -1);
      numberOfProfiles++;
  }

  // Load all profiles into memory
  iccFileArray = new QString[numberOfProfiles*numberOfScreens];
  for (i=0;i<(base->iccProfileList->count());i++) {
      config->setGroup(base->iccProfileList->text(i));
      for (j=0;j<(base->randrScreenList->count());j++) {
          iccFileArray[(i*(base->randrScreenList->count()))+j] = config->readEntry(base->randrScreenList->text(j));
      }
  }

  if ((findProfileIndex(base->iccProfileList->currentText()) >= 0) && (findScreenIndex(base->randrScreenList->currentText()) >= 0)) {
      base->iccFile->setURL(iccFileArray[(findProfileIndex(base->iccProfileList->currentText())*base->randrScreenList->count())+findScreenIndex(base->randrScreenList->currentText())]);
  }
  else {
      base->iccFile->setURL("");
  }

  systemconfig->setGroup(NULL);
  base->systemEnableSupport->setChecked(systemconfig->readBoolEntry("EnableICC", false));
  base->systemIccFile->setEnabled(systemconfig->readBoolEntry("EnableICC", false));
  base->systemIccFile->setURL(systemconfig->readEntry("ICCFile"));

  delete randrsimple;

  emit changed(useDefaults);
}

void KICCConfig::save()
{
	int i;
	int j;
	KRandrSimpleAPI *randrsimple = new KRandrSimpleAPI::KRandrSimpleAPI();

	// Write system configuration
	systemconfig->setGroup(NULL);
	systemconfig->writeEntry("EnableICC", base->systemEnableSupport->isChecked());
	systemconfig->writeEntry("ICCFile", base->systemIccFile->url());

	// Write user configuration
	config->setGroup(NULL);
	config->writeEntry("DefaultProfile", m_defaultProfile);
	config->writeEntry("EnableICC", base->enableSupport->isChecked());

	// Save all profiles to disk
	for (i=0;i<(base->iccProfileList->count());i++) {
		config->setGroup(base->iccProfileList->text(i));
		for (j=0;j<(base->randrScreenList->count());j++) {
			config->writeEntry(base->randrScreenList->text(j), iccFileArray[(i*(base->randrScreenList->count()))+j]);
		}
	}

	config->sync();
	systemconfig->sync();

	TQString errorstr;
	if (base->enableSupport->isChecked() == true) {
		errorstr = randrsimple->applyIccConfiguration(base->iccProfileList->currentText(), KDE_CONFDIR);
	}
	else if (base->systemEnableSupport->isChecked() == true) {
		errorstr = randrsimple->applySystemWideIccConfiguration(KDE_CONFDIR);
	}
	else {
		errorstr = randrsimple->clearIccConfiguration();
	}
	if (errorstr != "") {
		KMessageBox::error(this, TQString("Unable to apply ICC configuration:\n\r%1").arg(errorstr));
	}

	emit changed(false);
}

void KICCConfig::defaults()
{
	load( true );
}

TQString KICCConfig::quickHelp() const
{
  return i18n("<h1>ICC Profile Configuration</h1> This module allows you to configure KDE support"
     " for ICC profiles. This allows you to easily color correct your monitor"
     " for a more lifelike and vibrant image.");
}

#include "iccconfig.moc"