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.

508 lines
13 KiB

  1. /* vi: ts=8 sts=4 sw=4
  2. This file is part of the KDE project, module kcmbackground.
  3. Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
  4. Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. version 2 as published by the Free Software Foundation.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <tqbuttongroup.h>
  18. #include <tqcheckbox.h>
  19. #include <tqheader.h>
  20. #include <tqlabel.h>
  21. #include <tqlayout.h>
  22. #include <tqlineedit.h>
  23. #include <tqcombobox.h>
  24. #include <tqpushbutton.h>
  25. #include <tqslider.h>
  26. #include <tqspinbox.h>
  27. #include <tqwhatsthis.h>
  28. #include <tdeconfig.h>
  29. #include <kcolorbutton.h>
  30. #include <tdelocale.h>
  31. #include <tdemessagebox.h>
  32. #include <kpixmap.h>
  33. #include <kstandarddirs.h>
  34. #include <twin.h>
  35. #include "bgrender.h"
  36. #include "bgadvanced.h"
  37. #include "bgadvanced_ui.h"
  38. #include <X11/Xlib.h>
  39. /**** BGAdvancedDialog ****/
  40. static TQCString desktopConfigname()
  41. {
  42. int desktop=0;
  43. if (tqt_xdisplay())
  44. desktop = DefaultScreen(tqt_xdisplay());
  45. TQCString name;
  46. if (desktop == 0)
  47. name = "kdesktoprc";
  48. else
  49. name.sprintf("kdesktop-screen-%drc", desktop);
  50. return name;
  51. }
  52. BGAdvancedDialog::BGAdvancedDialog(KBackgroundRenderer *_r,
  53. TQWidget *parent,
  54. bool m_multidesktop)
  55. : KDialogBase(parent, "BGAdvancedDialog",
  56. true, i18n("Advanced Background Settings"),
  57. Ok | Cancel, Ok, true),
  58. r(_r)
  59. {
  60. dlg = new BGAdvancedBase(this);
  61. setMainWidget(dlg);
  62. dlg->m_listPrograms->header()->setStretchEnabled ( true, 1 );
  63. dlg->m_listPrograms->setAllColumnsShowFocus(true);
  64. connect(dlg->m_listPrograms, TQT_SIGNAL(clicked(TQListViewItem *)),
  65. TQT_SLOT(slotProgramItemClicked(TQListViewItem *)));
  66. // Load programs
  67. TQStringList lst = KBackgroundProgram::list();
  68. TQStringList::Iterator it;
  69. for (it=lst.begin(); it != lst.end(); ++it)
  70. addProgram(*it);
  71. if (m_multidesktop)
  72. {
  73. TDEConfig cfg(desktopConfigname(), false, false);
  74. cfg.setGroup( "General" );
  75. if (!cfg.readBoolEntry( "Enabled", true ))
  76. {
  77. dlg->m_groupIconText->hide();
  78. }
  79. dlg->m_spinCache->setSteps(512, 1024);
  80. dlg->m_spinCache->setRange(0, 40960);
  81. dlg->m_spinCache->setSpecialValueText(i18n("Unlimited"));
  82. dlg->m_spinCache->setSuffix(i18n(" KB"));
  83. connect(dlg->m_buttonAdd, TQT_SIGNAL(clicked()),
  84. TQT_SLOT(slotAdd()));
  85. connect(dlg->m_buttonRemove, TQT_SIGNAL(clicked()),
  86. TQT_SLOT(slotRemove()));
  87. connect(dlg->m_buttonModify, TQT_SIGNAL(clicked()),
  88. TQT_SLOT(slotModify()));
  89. connect(dlg->m_listPrograms, TQT_SIGNAL(doubleClicked(TQListViewItem *)),
  90. TQT_SLOT(slotProgramItemDoubleClicked(TQListViewItem *)));
  91. }
  92. else
  93. {
  94. dlg->m_buttonAdd->hide();
  95. dlg->m_buttonRemove->hide();
  96. dlg->m_buttonModify->hide();
  97. dlg->m_groupIconText->hide();
  98. dlg->m_groupCache->hide();
  99. }
  100. connect( dlg->m_cbProgram, TQT_SIGNAL(toggled(bool)),
  101. TQT_SLOT(slotEnableProgram(bool)));
  102. m_backgroundMode = m_oldBackgroundMode = r->backgroundMode();
  103. if (m_oldBackgroundMode == KBackgroundSettings::Program)
  104. m_oldBackgroundMode = KBackgroundSettings::Flat;
  105. dlg->adjustSize();
  106. updateUI();
  107. }
  108. void BGAdvancedDialog::makeReadOnly()
  109. {
  110. dlg->m_cbProgram->setEnabled(false);
  111. dlg->m_listPrograms->setEnabled(false);
  112. }
  113. void BGAdvancedDialog::setCacheSize(int s)
  114. {
  115. dlg->m_spinCache->setValue(s);
  116. }
  117. int BGAdvancedDialog::cacheSize()
  118. {
  119. return dlg->m_spinCache->value();
  120. }
  121. TQColor BGAdvancedDialog::textColor()
  122. {
  123. return dlg->m_colorText->color();
  124. }
  125. void BGAdvancedDialog::setTextColor(const TQColor &color)
  126. {
  127. dlg->m_colorText->setColor(color);
  128. }
  129. TQColor BGAdvancedDialog::textBackgroundColor()
  130. {
  131. return dlg->m_cbSolidTextBackground->isChecked() ?
  132. dlg->m_colorTextBackground->color() : TQColor();
  133. }
  134. void BGAdvancedDialog::setTextBackgroundColor(const TQColor &color)
  135. {
  136. dlg->m_colorTextBackground->blockSignals(true);
  137. dlg->m_cbSolidTextBackground->blockSignals(true);
  138. if (color.isValid())
  139. {
  140. dlg->m_cbSolidTextBackground->setChecked(true);
  141. dlg->m_colorTextBackground->setColor(color);
  142. dlg->m_colorTextBackground->setEnabled(true);
  143. }
  144. else
  145. {
  146. dlg->m_cbSolidTextBackground->setChecked(false);
  147. dlg->m_colorTextBackground->setColor(Qt::white);
  148. dlg->m_colorTextBackground->setEnabled(false);
  149. }
  150. dlg->m_colorTextBackground->blockSignals(false);
  151. dlg->m_cbSolidTextBackground->blockSignals(false);
  152. }
  153. bool BGAdvancedDialog::shadowEnabled()
  154. {
  155. return dlg->m_cbShadow->isChecked();
  156. }
  157. void BGAdvancedDialog::setShadowEnabled(bool enabled)
  158. {
  159. dlg->m_cbShadow->setChecked(enabled);
  160. }
  161. void BGAdvancedDialog::setTextLines(int lines)
  162. {
  163. dlg->m_spinTextLines->setValue(lines);
  164. }
  165. int BGAdvancedDialog::textLines() const
  166. {
  167. return dlg->m_spinTextLines->value();
  168. }
  169. void BGAdvancedDialog::setTextWidth(int width)
  170. {
  171. dlg->m_spinTextWidth->setValue(width);
  172. }
  173. int BGAdvancedDialog::textWidth() const
  174. {
  175. return dlg->m_spinTextWidth->value();
  176. }
  177. void BGAdvancedDialog::updateUI()
  178. {
  179. TQString prog = r->KBackgroundProgram::name();
  180. dlg->m_cbProgram->blockSignals(true);
  181. if ((r->backgroundMode() == KBackgroundSettings::Program)
  182. && !prog.isEmpty())
  183. {
  184. dlg->m_cbProgram->setChecked(true);
  185. dlg->m_listPrograms->setEnabled(true);
  186. dlg->m_buttonAdd->setEnabled(true);
  187. dlg->m_buttonRemove->setEnabled(true);
  188. dlg->m_buttonModify->setEnabled(true);
  189. selectProgram(prog);
  190. }
  191. else
  192. {
  193. dlg->m_cbProgram->setChecked(false);
  194. dlg->m_listPrograms->setEnabled(false);
  195. dlg->m_buttonAdd->setEnabled(false);
  196. dlg->m_buttonRemove->setEnabled(false);
  197. dlg->m_buttonModify->setEnabled(false);
  198. }
  199. dlg->m_cbProgram->blockSignals(false);
  200. }
  201. void BGAdvancedDialog::removeProgram(const TQString &name)
  202. {
  203. if (m_programItems.find(name))
  204. {
  205. delete m_programItems[name];
  206. m_programItems.remove(name);
  207. }
  208. }
  209. void BGAdvancedDialog::addProgram(const TQString &name)
  210. {
  211. removeProgram(name);
  212. KBackgroundProgram prog(name);
  213. if (prog.command().isEmpty() || (prog.isGlobal() && !prog.isAvailable()))
  214. return;
  215. TQListViewItem *item = new TQListViewItem(dlg->m_listPrograms);
  216. item->setText(0, prog.name());
  217. item->setText(1, prog.comment());
  218. item->setText(2, i18n("%1 min.").arg(prog.refresh()));
  219. m_programItems.insert(name, item);
  220. }
  221. void BGAdvancedDialog::selectProgram(const TQString &name)
  222. {
  223. if (m_programItems.find(name))
  224. {
  225. TQListViewItem *item = m_programItems[name];
  226. dlg->m_listPrograms->ensureItemVisible(item);
  227. dlg->m_listPrograms->setSelected(item, true);
  228. m_selectedProgram = name;
  229. }
  230. }
  231. void BGAdvancedDialog::slotAdd()
  232. {
  233. KProgramEditDialog dlg;
  234. dlg.exec();
  235. if (dlg.result() == TQDialog::Accepted)
  236. {
  237. TQString program = dlg.program();
  238. addProgram(program);
  239. selectProgram(program);
  240. }
  241. }
  242. void BGAdvancedDialog::slotRemove()
  243. {
  244. if (m_selectedProgram.isEmpty())
  245. return;
  246. KBackgroundProgram prog(m_selectedProgram);
  247. if (prog.isGlobal())
  248. {
  249. KMessageBox::sorry(this,
  250. i18n("Unable to remove the program: the program is global "
  251. "and can only be removed by the system administrator."),
  252. i18n("Cannot Remove Program"));
  253. return;
  254. }
  255. if (KMessageBox::warningContinueCancel(this,
  256. i18n("Are you sure you want to remove the program `%1'?")
  257. .arg(prog.name()),
  258. i18n("Remove Background Program"),
  259. i18n("&Remove")) != KMessageBox::Continue)
  260. return;
  261. prog.remove();
  262. removeProgram(m_selectedProgram);
  263. m_selectedProgram = TQString::null;
  264. }
  265. /*
  266. * Modify a program.
  267. */
  268. void BGAdvancedDialog::slotModify()
  269. {
  270. if (m_selectedProgram.isEmpty())
  271. return;
  272. KProgramEditDialog dlg(m_selectedProgram);
  273. dlg.exec();
  274. if (dlg.result() == TQDialog::Accepted)
  275. {
  276. TQString program = dlg.program();
  277. if (program != m_selectedProgram)
  278. {
  279. KBackgroundProgram prog(m_selectedProgram);
  280. prog.remove();
  281. removeProgram(m_selectedProgram);
  282. }
  283. addProgram(dlg.program());
  284. selectProgram(dlg.program());
  285. }
  286. }
  287. void BGAdvancedDialog::slotProgramItemClicked(TQListViewItem *item)
  288. {
  289. if (item)
  290. m_selectedProgram = item->text(0);
  291. slotProgramChanged();
  292. }
  293. void BGAdvancedDialog::slotProgramItemDoubleClicked(TQListViewItem *item)
  294. {
  295. slotProgramItemClicked(item);
  296. slotModify();
  297. }
  298. void BGAdvancedDialog::slotProgramChanged()
  299. {
  300. if (dlg->m_cbProgram->isChecked() && !m_selectedProgram.isEmpty())
  301. m_backgroundMode = KBackgroundSettings::Program;
  302. else
  303. m_backgroundMode = m_oldBackgroundMode;
  304. }
  305. void BGAdvancedDialog::slotEnableProgram(bool b)
  306. {
  307. dlg->m_listPrograms->setEnabled(b);
  308. if (b)
  309. {
  310. dlg->m_listPrograms->blockSignals(true);
  311. TQListViewItem *cur = dlg->m_listPrograms->currentItem();
  312. dlg->m_listPrograms->setSelected(cur, true);
  313. dlg->m_listPrograms->ensureItemVisible(cur);
  314. dlg->m_listPrograms->blockSignals(false);
  315. slotProgramItemClicked(cur);
  316. }
  317. else
  318. {
  319. slotProgramChanged();
  320. }
  321. }
  322. TQString BGAdvancedDialog::backgroundProgram() const
  323. {
  324. return m_selectedProgram;
  325. }
  326. int BGAdvancedDialog::backgroundMode() const
  327. {
  328. return m_backgroundMode;
  329. }
  330. /**** KProgramEditDialog ****/
  331. KProgramEditDialog::KProgramEditDialog(const TQString &program, TQWidget *parent, char *name)
  332. : KDialogBase(parent, name, true, i18n("Configure Background Program"),
  333. Ok | Cancel, Ok, true)
  334. {
  335. TQFrame *frame = makeMainWidget();
  336. TQGridLayout *grid = new TQGridLayout(frame, 6, 2, 0, spacingHint());
  337. grid->addColSpacing(1, 300);
  338. TQLabel *lbl = new TQLabel(i18n("&Name:"), frame);
  339. grid->addWidget(lbl, 0, 0);
  340. m_NameEdit = new TQLineEdit(frame);
  341. lbl->setBuddy(m_NameEdit);
  342. grid->addWidget(m_NameEdit, 0, 1);
  343. lbl = new TQLabel(i18n("Co&mment:"), frame);
  344. grid->addWidget(lbl, 1, 0);
  345. m_CommentEdit = new TQLineEdit(frame);
  346. lbl->setBuddy(m_CommentEdit);
  347. grid->addWidget(m_CommentEdit, 1, 1);
  348. lbl = new TQLabel(i18n("Comman&d:"), frame);
  349. grid->addWidget(lbl, 2, 0);
  350. m_CommandEdit = new TQLineEdit(frame);
  351. lbl->setBuddy(m_CommandEdit);
  352. grid->addWidget(m_CommandEdit, 2, 1);
  353. lbl = new TQLabel(i18n("&Preview cmd:"), frame);
  354. grid->addWidget(lbl, 3, 0);
  355. m_PreviewEdit = new TQLineEdit(frame);
  356. lbl->setBuddy(m_PreviewEdit);
  357. grid->addWidget(m_PreviewEdit, 3, 1);
  358. lbl = new TQLabel(i18n("&Executable:"), frame);
  359. grid->addWidget(lbl, 4, 0);
  360. m_ExecEdit = new TQLineEdit(frame);
  361. lbl->setBuddy(m_ExecEdit);
  362. grid->addWidget(m_ExecEdit, 4, 1);
  363. lbl = new TQLabel(i18n("&Refresh time:"), frame);
  364. grid->addWidget(lbl, 5, 0);
  365. m_RefreshEdit = new TQSpinBox(frame);
  366. m_RefreshEdit->setRange(5, 60);
  367. m_RefreshEdit->setSteps(5, 10);
  368. m_RefreshEdit->setSuffix(i18n(" min"));
  369. m_RefreshEdit->setFixedSize(m_RefreshEdit->sizeHint());
  370. lbl->setBuddy(m_RefreshEdit);
  371. grid->addWidget(m_RefreshEdit, 5, 1, Qt::AlignLeft);
  372. m_Program = program;
  373. if (m_Program.isEmpty()) {
  374. KBackgroundProgram prog(i18n("New Command"));
  375. int i = 1;
  376. while (!prog.command().isEmpty())
  377. prog.load(i18n("New Command <%1>").arg(i++));
  378. m_NameEdit->setText(prog.name());
  379. m_NameEdit->setSelection(0, 100);
  380. m_RefreshEdit->setValue(15);
  381. return;
  382. }
  383. // Fill in the fields
  384. m_NameEdit->setText(m_Program);
  385. KBackgroundProgram prog(m_Program);
  386. m_CommentEdit->setText(prog.comment());
  387. m_ExecEdit->setText(prog.executable());
  388. m_CommandEdit->setText(prog.command());
  389. m_PreviewEdit->setText(prog.previewCommand());
  390. m_RefreshEdit->setValue(prog.refresh());
  391. }
  392. TQString KProgramEditDialog::program()const
  393. {
  394. return m_NameEdit->text();
  395. }
  396. void KProgramEditDialog::slotOk()
  397. {
  398. TQString s = m_NameEdit->text();
  399. if (s.isEmpty()) {
  400. KMessageBox::sorry(this, i18n("You did not fill in the `Name' field.\n"
  401. "This is a required field."));
  402. return;
  403. }
  404. KBackgroundProgram prog(s);
  405. if ((s != m_Program) && !prog.command().isEmpty()) {
  406. int ret = KMessageBox::warningContinueCancel(this,
  407. i18n("There is already a program with the name `%1'.\n"
  408. "Do you want to overwrite it?").arg(s),TQString::null,i18n("Overwrite"));
  409. if (ret != KMessageBox::Continue)
  410. return;
  411. }
  412. if (m_ExecEdit->text().isEmpty()) {
  413. KMessageBox::sorry(this, i18n("You did not fill in the `Executable' field.\n"
  414. "This is a required field."));
  415. return;
  416. }
  417. if (m_CommandEdit->text().isEmpty()) {
  418. KMessageBox::sorry(this, i18n("You did not fill in the `Command' field.\n"
  419. "This is a required field."));
  420. return;
  421. }
  422. prog.setComment(m_CommentEdit->text());
  423. prog.setExecutable(m_ExecEdit->text());
  424. prog.setCommand(m_CommandEdit->text());
  425. prog.setPreviewCommand(m_PreviewEdit->text());
  426. prog.setRefresh(m_RefreshEdit->value());
  427. prog.writeSettings();
  428. accept();
  429. }
  430. #include "bgadvanced.moc"