TDE base libraries and programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iccconfig.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * iccconfig.cpp
  3. *
  4. * Copyright (c) 2009-2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <tqcheckbox.h>
  21. #include <tqlabel.h>
  22. #include <tqlayout.h>
  23. #include <tqlineedit.h>
  24. #include <tqpushbutton.h>
  25. #include <dcopclient.h>
  26. #include <tdeaboutdata.h>
  27. #include <tdeapplication.h>
  28. #include <tdeconfig.h>
  29. #include <kcombobox.h>
  30. #include <kdebug.h>
  31. #include <kdialog.h>
  32. #include <tdeglobal.h>
  33. #include <tdelistview.h>
  34. #include <tdelocale.h>
  35. #include <tdemessagebox.h>
  36. #include <tdepopupmenu.h>
  37. #include <kinputdialog.h>
  38. #include <kurlrequester.h>
  39. #include <kgenericfactory.h>
  40. #include <unistd.h>
  41. #include <ksimpleconfig.h>
  42. #include <string>
  43. #include <stdio.h>
  44. #include <tqstring.h>
  45. #include "iccconfig.h"
  46. using namespace std;
  47. /**** DLL Interface ****/
  48. typedef KGenericFactory<KICCConfig, TQWidget> KICCCFactory;
  49. K_EXPORT_COMPONENT_FACTORY( kcm_iccconfig, KICCCFactory("kcmiccconfig") )
  50. KSimpleConfig *config;
  51. KSimpleConfig *systemconfig;
  52. /**** KICCConfig ****/
  53. KICCConfig::KICCConfig(TQWidget *parent, const char *name, const TQStringList &)
  54. : TDECModule(KICCCFactory::instance(), parent, name), iccFileArray(NULL)
  55. {
  56. TQVBoxLayout *layout = new TQVBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint());
  57. if (getuid() != 0) {
  58. config = new KSimpleConfig( TQString::fromLatin1( "kiccconfigrc" ));
  59. }
  60. else {
  61. config = NULL;
  62. }
  63. systemconfig = new KSimpleConfig( TQString::fromLatin1( KDE_CONFDIR "/kicc/kiccconfigrc" ));
  64. TDEAboutData *about =
  65. new TDEAboutData(I18N_NOOP("kcmiccconfig"), I18N_NOOP("TDE ICC Profile Control Module"),
  66. 0, 0, TDEAboutData::License_GPL,
  67. I18N_NOOP("(c) 2009,2010 Timothy Pearson"));
  68. about->addAuthor("Timothy Pearson", 0, "kb9vqf@pearsoncomputing.net");
  69. setAboutData( about );
  70. base = new ICCConfigBase(this);
  71. layout->add(base);
  72. 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."));
  73. // setUseRootOnlyMsg(true); // Setting this hides the Apply button!
  74. connect(base->systemEnableSupport, TQT_SIGNAL(clicked()), TQT_SLOT(changed()));
  75. connect(base->systemEnableSupport, TQT_SIGNAL(toggled(bool)), base->systemIccFile, TQT_SLOT(setEnabled(bool)));
  76. connect(base->enableSupport, TQT_SIGNAL(clicked()), TQT_SLOT(changed()));
  77. connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->iccFile, TQT_SLOT(setEnabled(bool)));
  78. connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->randrScreenList, TQT_SLOT(setEnabled(bool)));
  79. connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->iccProfileList, TQT_SLOT(setEnabled(bool)));
  80. connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->addProfileButton, TQT_SLOT(setEnabled(bool)));
  81. connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->renameProfileButton, TQT_SLOT(setEnabled(bool)));
  82. connect(base->enableSupport, TQT_SIGNAL(toggled(bool)), base->deleteProfileButton, TQT_SLOT(setEnabled(bool)));
  83. connect(base->iccProfileList, TQT_SIGNAL(activated(int)), this, TQT_SLOT(selectProfile(int)));
  84. connect(base->randrScreenList, TQT_SIGNAL(activated(int)), this, TQT_SLOT(selectScreen(int)));
  85. connect(base->iccFile, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(updateArray()));
  86. connect(base->systemIccFile, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(changed()));
  87. connect(base->addProfileButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addProfile()));
  88. connect(base->renameProfileButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(renameProfile()));
  89. connect(base->deleteProfileButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteProfile()));
  90. load();
  91. if (!config || !config->checkConfigFilesWritable( true )) {
  92. base->enableSupport->setEnabled(false);
  93. base->randrScreenList->setEnabled(false);
  94. base->iccProfileList->setEnabled(false);
  95. base->iccFile->setEnabled(false);
  96. base->addProfileButton->setEnabled(false);
  97. base->renameProfileButton->setEnabled(false);
  98. base->deleteProfileButton->setEnabled(false);
  99. }
  100. if (getuid() != 0 || !systemconfig->checkConfigFilesWritable( true )) {
  101. base->systemEnableSupport->setEnabled(false);
  102. base->systemIccFile->setEnabled(false);
  103. }
  104. }
  105. KICCConfig::~KICCConfig()
  106. {
  107. if (iccFileArray) {
  108. delete [] iccFileArray;
  109. }
  110. if (config) {
  111. delete config;
  112. }
  113. delete systemconfig;
  114. }
  115. void KICCConfig::deleteProfile () {
  116. int i;
  117. TQString *iccFileArrayNew;
  118. // Delete the profile
  119. if (config) config->deleteGroup(base->iccProfileList->currentText());
  120. base->iccProfileList->removeItem(base->iccProfileList->currentItem());
  121. base->iccProfileList->setCurrentItem(base->iccProfileList->count()-1);
  122. // Contract the profile memory
  123. numberOfProfiles--;
  124. iccFileArrayNew = new TQString[numberOfProfiles*numberOfScreens];
  125. for (i=0;i<(numberOfProfiles*numberOfScreens);i++) {
  126. iccFileArrayNew[i] = iccFileArray[i];
  127. }
  128. delete [] iccFileArray;
  129. iccFileArray = iccFileArrayNew;
  130. }
  131. void KICCConfig::renameProfile () {
  132. int i;
  133. TQString *iccFileArrayNew;
  134. // Pop up a text entry box asking for the name of the new profile
  135. bool _ok = false;
  136. bool _end = false;
  137. TQString _new;
  138. TQString _text = i18n("Please enter the new profile name below:");
  139. TQString _error;
  140. while (!_end) {
  141. _new = KInputDialog::getText( i18n("ICC Profile Configuration"), _error + _text, TQString::null, &_ok, this);
  142. if (!_ok ) {
  143. _end = true;
  144. } else {
  145. _error = TQString();
  146. if (!_new.isEmpty()) {
  147. if (findProfileIndex(_new) != -1)
  148. _error = i18n("Error: A profile with that name already exists") + TQString("\n");
  149. else
  150. _end = true;
  151. }
  152. }
  153. }
  154. if (!_ok) return;
  155. // Rename the profile
  156. if (config) config->deleteGroup(base->iccProfileList->currentText());
  157. base->iccProfileList->changeItem(_new, base->iccProfileList->currentItem());
  158. updateDisplayedInformation();
  159. emit changed();
  160. }
  161. void KICCConfig::addProfile () {
  162. int i;
  163. TQString *iccFileArrayNew;
  164. // Pop up a text entry box asking for the name of the new profile
  165. bool _ok = false;
  166. bool _end = false;
  167. TQString _new;
  168. TQString _text = i18n("Please enter the new profile name below:");
  169. TQString _error;
  170. while (!_end) {
  171. _new = KInputDialog::getText( i18n("ICC Profile Configuration"), _error + _text, TQString::null, &_ok, this);
  172. if (!_ok ) {
  173. _end = true;
  174. } else {
  175. _error = TQString();
  176. if (!_new.isEmpty()) {
  177. if (findProfileIndex(_new) != -1)
  178. _error = i18n("Error: A profile with that name already exists") + TQString("\n");
  179. else
  180. _end = true;
  181. }
  182. }
  183. }
  184. if (!_ok) return;
  185. // Expand the profile memory
  186. numberOfProfiles++;
  187. iccFileArrayNew = new TQString[numberOfProfiles*numberOfScreens];
  188. for (i=0;i<((numberOfProfiles-1)*numberOfScreens);i++) {
  189. iccFileArrayNew[i] = iccFileArray[i];
  190. }
  191. delete [] iccFileArray;
  192. iccFileArray = iccFileArrayNew;
  193. for (;i<(numberOfProfiles*numberOfScreens);i++) {
  194. iccFileArray[i] = "";
  195. }
  196. // Insert the new profile name
  197. base->iccProfileList->insertItem(_new, -1);
  198. base->iccProfileList->setCurrentItem(base->iccProfileList->count()-1);
  199. updateDisplayedInformation();
  200. emit changed();
  201. }
  202. void KICCConfig::load()
  203. {
  204. load( false );
  205. }
  206. void KICCConfig::selectProfile (int slotNumber) {
  207. updateDisplayedInformation();
  208. emit changed();
  209. }
  210. void KICCConfig::selectScreen (int slotNumber) {
  211. updateDisplayedInformation();
  212. }
  213. void KICCConfig::updateArray (void) {
  214. iccFileArray[((base->iccProfileList->currentItem())*(base->randrScreenList->count()))+(base->randrScreenList->currentItem())] = base->iccFile->url();
  215. if (config) {
  216. config->setGroup(base->iccProfileList->currentText());
  217. if (config->readEntry(base->randrScreenList->currentText()) != iccFileArray[((base->iccProfileList->currentItem())*(base->randrScreenList->count()))+(base->randrScreenList->currentItem())]) {
  218. emit changed();
  219. }
  220. }
  221. }
  222. void KICCConfig::updateDisplayedInformation () {
  223. base->iccFile->setURL(iccFileArray[((base->iccProfileList->currentItem())*(base->randrScreenList->count()))+(base->randrScreenList->currentItem())]);
  224. }
  225. TQString KICCConfig::extractFileName(TQString displayName, TQString profileName) {
  226. //
  227. }
  228. int KICCConfig::findProfileIndex(TQString profileName) {
  229. int i;
  230. for (i=0;i<numberOfProfiles;i++) {
  231. if (base->iccProfileList->text(i) == profileName) {
  232. return i;
  233. }
  234. }
  235. return -1;
  236. }
  237. int KICCConfig::findScreenIndex(TQString screenName) {
  238. int i;
  239. for (i=0;i<(base->randrScreenList->count());i++) {
  240. if (base->randrScreenList->text(i) == screenName) {
  241. return i;
  242. }
  243. }
  244. return -1;
  245. }
  246. void KICCConfig::load(bool useDefaults )
  247. {
  248. //Update the toggle buttons with the current configuration
  249. int i;
  250. int j;
  251. // FIXME Should use font size (basically resultant string length) to set button widths...
  252. base->addProfileButton->setFixedWidth(110);
  253. base->renameProfileButton->setFixedWidth(90);
  254. base->deleteProfileButton->setFixedWidth(90);
  255. XRROutputInfo *output_info;
  256. KRandrSimpleAPI *randrsimple = new KRandrSimpleAPI();
  257. if (config) {
  258. config->setReadDefaults( useDefaults );
  259. config->setGroup(NULL);
  260. base->enableSupport->setChecked(config->readBoolEntry("EnableICC", false));
  261. base->randrScreenList->setEnabled(config->readBoolEntry("EnableICC", false));
  262. base->iccProfileList->setEnabled(config->readBoolEntry("EnableICC", false));
  263. base->iccFile->setEnabled(config->readBoolEntry("EnableICC", false));
  264. base->addProfileButton->setEnabled(config->readBoolEntry("EnableICC", false));
  265. base->renameProfileButton->setEnabled(config->readBoolEntry("EnableICC", false));
  266. base->deleteProfileButton->setEnabled(config->readBoolEntry("EnableICC", false));
  267. numberOfScreens = 0;
  268. if (randrsimple->isValid() == true) {
  269. randr_display = XOpenDisplay(NULL);
  270. randr_screen_info = randrsimple->read_screen_info(randr_display);
  271. for (i = 0; i < randr_screen_info->n_output; i++) {
  272. output_info = randr_screen_info->outputs[i]->info;
  273. base->randrScreenList->insertItem(output_info->name, -1);
  274. numberOfScreens++;
  275. }
  276. }
  277. else {
  278. base->randrScreenList->insertItem("Default", -1);
  279. numberOfScreens++;
  280. }
  281. // Find all profile names
  282. numberOfProfiles = 0;
  283. cfgProfiles = config->groupList();
  284. for (TQStringList::Iterator i(cfgProfiles.begin()); i != cfgProfiles.end(); ++i) {
  285. base->iccProfileList->insertItem((*i), -1);
  286. numberOfProfiles++;
  287. }
  288. if (numberOfProfiles == 0) {
  289. base->iccProfileList->insertItem("<default>", -1);
  290. numberOfProfiles++;
  291. }
  292. // Load all profiles into memory
  293. iccFileArray = new TQString[numberOfProfiles*numberOfScreens];
  294. for (i=0;i<(base->iccProfileList->count());i++) {
  295. config->setGroup(base->iccProfileList->text(i));
  296. for (j=0;j<(base->randrScreenList->count());j++) {
  297. iccFileArray[(i*(base->randrScreenList->count()))+j] = config->readEntry(base->randrScreenList->text(j));
  298. }
  299. }
  300. if ((findProfileIndex(base->iccProfileList->currentText()) >= 0) && (findScreenIndex(base->randrScreenList->currentText()) >= 0)) {
  301. base->iccFile->setURL(iccFileArray[(findProfileIndex(base->iccProfileList->currentText())*base->randrScreenList->count())+findScreenIndex(base->randrScreenList->currentText())]);
  302. }
  303. else {
  304. base->iccFile->setURL("");
  305. }
  306. }
  307. else {
  308. base->enableSupport->setChecked(false);
  309. base->randrScreenList->setEnabled(false);
  310. base->iccProfileList->setEnabled(false);
  311. base->iccFile->setEnabled(false);
  312. base->addProfileButton->setEnabled(false);
  313. base->renameProfileButton->setEnabled(false);
  314. base->deleteProfileButton->setEnabled(false);
  315. }
  316. systemconfig->setGroup(NULL);
  317. base->systemEnableSupport->setChecked(systemconfig->readBoolEntry("EnableICC", false));
  318. base->systemIccFile->setEnabled(systemconfig->readBoolEntry("EnableICC", false));
  319. base->systemIccFile->setURL(systemconfig->readEntry("ICCFile"));
  320. delete randrsimple;
  321. emit changed(useDefaults);
  322. }
  323. void KICCConfig::save()
  324. {
  325. int i;
  326. int j;
  327. KRandrSimpleAPI *randrsimple = new KRandrSimpleAPI();
  328. // Write system configuration
  329. systemconfig->setGroup(NULL);
  330. systemconfig->writeEntry("EnableICC", base->systemEnableSupport->isChecked());
  331. systemconfig->writeEntry("ICCFile", base->systemIccFile->url());
  332. systemconfig->sync();
  333. if (config) {
  334. // Write user configuration
  335. config->setGroup(NULL);
  336. config->writeEntry("DefaultProfile", m_defaultProfile);
  337. config->writeEntry("EnableICC", base->enableSupport->isChecked());
  338. // Save all profiles to disk
  339. for (i=0;i<(base->iccProfileList->count());i++) {
  340. config->setGroup(base->iccProfileList->text(i));
  341. for (j=0;j<(base->randrScreenList->count());j++) {
  342. config->writeEntry(base->randrScreenList->text(j), iccFileArray[(i*(base->randrScreenList->count()))+j]);
  343. }
  344. }
  345. config->sync();
  346. }
  347. TQString errorstr;
  348. if (base->enableSupport->isChecked() == true) {
  349. errorstr = randrsimple->applyIccConfiguration(base->iccProfileList->currentText(), KDE_CONFDIR);
  350. }
  351. else if (base->systemEnableSupport->isChecked() == true) {
  352. errorstr = randrsimple->applySystemWideIccConfiguration(KDE_CONFDIR);
  353. }
  354. else {
  355. errorstr = randrsimple->clearIccConfiguration();
  356. }
  357. if (errorstr != "") {
  358. KMessageBox::error(this, TQString("Unable to apply ICC configuration:\n%1").arg(errorstr));
  359. }
  360. emit changed(false);
  361. }
  362. void KICCConfig::defaults()
  363. {
  364. load( true );
  365. }
  366. TQString KICCConfig::quickHelp() const
  367. {
  368. return i18n("<h1>ICC Profile Configuration</h1> This module allows you to configure TDE support"
  369. " for ICC profiles. This allows you to easily color correct your monitor"
  370. " for a more lifelike and vibrant image.");
  371. }
  372. #include "iccconfig.moc"