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.

1282 lines
32 KiB

  1. /* vi: ts=8 sts=4 sw=4
  2. * kate: space-indent on; indent-width 4; indent-mode cstyle;
  3. *
  4. * This file is part of the KDE project, module kdesktop.
  5. * Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
  6. *
  7. * You can Freely distribute this program under the GNU Library General
  8. * Public License. See the file "COPYING.LIB" for the exact licensing terms.
  9. */
  10. #include <netwm.h>
  11. #include <time.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. // Clean up after X.h/Xlib.h
  15. #undef Bool
  16. #undef Unsorted
  17. #include <tqdir.h>
  18. #include <tqpixmap.h>
  19. #include <dcopclient.h>
  20. #include <tdeapplication.h>
  21. #include <kdebug.h>
  22. #include <tdeglobalsettings.h>
  23. #include <ksimpleconfig.h>
  24. #include <kstandarddirs.h>
  25. #include <krandomsequence.h>
  26. #include <konq_defaults.h>
  27. #include "bgdefaults.h"
  28. #include "bghash.h"
  29. #include "bgsettings.h"
  30. /**** KBackgroundPattern ****/
  31. KBackgroundPattern::KBackgroundPattern(TQString name)
  32. {
  33. dirty = false;
  34. hashdirty = true;
  35. m_pDirs = TDEGlobal::dirs();
  36. m_pDirs->addResourceType("dtop_pattern", m_pDirs->kde_default("data") +
  37. "kdesktop/patterns");
  38. m_pConfig = 0L;
  39. m_Name = name;
  40. if (m_Name.isEmpty())
  41. return;
  42. init();
  43. readSettings();
  44. }
  45. KBackgroundPattern::~KBackgroundPattern()
  46. {
  47. delete m_pConfig;
  48. }
  49. void KBackgroundPattern::copyConfig(const KBackgroundPattern *settings)
  50. {
  51. dirty = hashdirty = true;
  52. m_Name = settings->m_Name;
  53. m_Comment = settings->m_Comment;
  54. m_Pattern = settings->m_Pattern;
  55. m_File = settings->m_File;
  56. }
  57. void KBackgroundPattern::load(TQString name)
  58. {
  59. m_Name = name;
  60. init();
  61. readSettings();
  62. }
  63. void KBackgroundPattern::init(bool force_rw)
  64. {
  65. delete m_pConfig;
  66. m_File = m_pDirs->findResource("dtop_pattern", m_Name + ".desktop");
  67. if (force_rw || m_File.isEmpty()) {
  68. m_File = m_pDirs->saveLocation("dtop_pattern") + m_Name + ".desktop";
  69. m_pConfig = new KSimpleConfig(m_File);
  70. } else
  71. m_pConfig = new KSimpleConfig(m_File);
  72. m_pConfig->setGroup("KDE Desktop Pattern");
  73. TQFileInfo fi(m_File);
  74. m_bReadOnly = !fi.isWritable();
  75. }
  76. void KBackgroundPattern::setComment(const TQString &comment)
  77. {
  78. if (m_Comment == comment)
  79. return;
  80. dirty = true;
  81. m_Comment = comment;
  82. }
  83. void KBackgroundPattern::setPattern(TQString pattern)
  84. {
  85. if (m_Pattern == pattern)
  86. return;
  87. dirty = hashdirty = true;
  88. m_Pattern = pattern;
  89. }
  90. void KBackgroundPattern::readSettings()
  91. {
  92. dirty = false;
  93. hashdirty = true;
  94. m_Pattern = m_pConfig->readPathEntry("File");
  95. m_Comment = m_pConfig->readEntry("Comment");
  96. if (m_Comment.isEmpty())
  97. m_Comment = m_File.mid(m_File.findRev('/')+1);
  98. }
  99. void KBackgroundPattern::writeSettings()
  100. {
  101. if (!dirty)
  102. return;
  103. if (m_bReadOnly)
  104. init(true);
  105. if ( !m_pConfig )
  106. return; // better safe than sorry
  107. m_pConfig->writePathEntry("File", m_Pattern);
  108. m_pConfig->writeEntry("Comment", m_Comment);
  109. m_pConfig->sync();
  110. dirty = false;
  111. }
  112. bool KBackgroundPattern::isAvailable()
  113. {
  114. if (m_Pattern.isEmpty())
  115. return false;
  116. TQString file = m_Pattern;
  117. if (file.at(0) != '/')
  118. file = m_pDirs->findResource("dtop_pattern", file);
  119. TQFileInfo fi(file);
  120. return (fi.exists());
  121. }
  122. bool KBackgroundPattern::remove()
  123. {
  124. if (m_bReadOnly)
  125. return false;
  126. return !unlink(TQFile::encodeName(m_File));
  127. }
  128. TQString KBackgroundPattern::fingerprint()
  129. {
  130. return m_File;
  131. }
  132. int KBackgroundPattern::hash()
  133. {
  134. if (hashdirty) {
  135. m_Hash = TQHash(fingerprint());
  136. hashdirty = false;
  137. }
  138. return m_Hash;
  139. }
  140. /* static */
  141. TQStringList KBackgroundPattern::list()
  142. {
  143. TDEStandardDirs *dirs = TDEGlobal::dirs();
  144. dirs->addResourceType("dtop_pattern", dirs->kde_default("data") +
  145. "kdesktop/patterns");
  146. TQStringList lst = dirs->findAllResources("dtop_pattern", "*.desktop",
  147. false, true);
  148. TQStringList::Iterator it;
  149. for (it=lst.begin(); it!=lst.end(); ++it) {
  150. // Strip path and suffix
  151. int pos = (*it).findRev('/');
  152. if (pos != -1)
  153. (*it) = (*it).mid(pos+1);
  154. pos = (*it).findRev('.');
  155. if (pos != -1)
  156. (*it) = (*it).left(pos);
  157. }
  158. return lst;
  159. }
  160. /**** KBackgroundProgram ****/
  161. KBackgroundProgram::KBackgroundProgram(TQString name)
  162. {
  163. dirty = false;
  164. hashdirty = true;
  165. m_pDirs = TDEGlobal::dirs();
  166. m_pDirs->addResourceType("dtop_program", m_pDirs->kde_default("data") +
  167. "kdesktop/programs");
  168. m_pConfig = 0L;
  169. // prevent updates when just constructed.
  170. m_LastChange = (int) time(0L);
  171. m_Name = name;
  172. if (m_Name.isEmpty())
  173. return;
  174. init();
  175. readSettings();
  176. }
  177. KBackgroundProgram::~KBackgroundProgram()
  178. {
  179. delete m_pConfig;
  180. }
  181. void KBackgroundProgram::copyConfig(const KBackgroundProgram *settings)
  182. {
  183. dirty = hashdirty = true;
  184. m_Refresh = settings->m_Refresh;
  185. m_LastChange = settings->m_LastChange;
  186. m_Name = settings->m_Name;
  187. m_Command = settings->m_Command;
  188. m_PreviewCommand = settings->m_PreviewCommand;
  189. m_Comment = settings->m_Comment;
  190. m_Executable = settings->m_Executable;
  191. m_File = settings->m_File;
  192. }
  193. void KBackgroundProgram::init(bool force_rw)
  194. {
  195. delete m_pConfig;
  196. m_File = m_pDirs->findResource("dtop_program", m_Name + ".desktop");
  197. if (force_rw || m_File.isEmpty()) {
  198. m_File = m_pDirs->saveLocation("dtop_program") + m_Name + ".desktop";
  199. m_pConfig = new KSimpleConfig(m_File);
  200. m_bReadOnly = false;
  201. } else {
  202. m_pConfig = new KSimpleConfig(m_File);
  203. m_bReadOnly = (m_File != locateLocal("dtop_program", m_Name + ".desktop"));
  204. }
  205. m_pConfig->setGroup("KDE Desktop Program");
  206. }
  207. void KBackgroundProgram::load(const TQString &name)
  208. {
  209. m_Name = name;
  210. init();
  211. readSettings();
  212. }
  213. void KBackgroundProgram::setComment(const TQString &comment)
  214. {
  215. if (m_Comment == comment)
  216. return;
  217. dirty = true;
  218. m_Comment = comment;
  219. }
  220. void KBackgroundProgram::setExecutable(const TQString &executable)
  221. {
  222. if (m_Executable == executable)
  223. return;
  224. dirty = true;
  225. m_Executable = executable;
  226. }
  227. void KBackgroundProgram::setCommand(const TQString &command)
  228. {
  229. if (m_Command == command)
  230. return;
  231. dirty = hashdirty = true;
  232. m_Command = command;
  233. }
  234. void KBackgroundProgram::setPreviewCommand(const TQString &command)
  235. {
  236. if (m_PreviewCommand == command)
  237. return;
  238. dirty = true;
  239. m_PreviewCommand = command;
  240. }
  241. void KBackgroundProgram::setRefresh(int refresh)
  242. {
  243. if (m_Refresh == refresh)
  244. return;
  245. dirty = hashdirty = true;
  246. m_Refresh = refresh;
  247. }
  248. void KBackgroundProgram::readSettings()
  249. {
  250. dirty = false;
  251. hashdirty = true;
  252. m_Comment = m_pConfig->readEntry("Comment");
  253. m_Executable = m_pConfig->readPathEntry("Executable");
  254. m_Command = m_pConfig->readPathEntry("Command");
  255. m_PreviewCommand = m_pConfig->readPathEntry("PreviewCommand", m_Command);
  256. m_Refresh = m_pConfig->readNumEntry("Refresh", 300);
  257. }
  258. void KBackgroundProgram::writeSettings()
  259. {
  260. if (!dirty)
  261. return;
  262. if (m_bReadOnly)
  263. init(true);
  264. if ( !m_pConfig )
  265. return; // better safe than sorry
  266. m_pConfig->writeEntry("Comment", m_Comment);
  267. m_pConfig->writePathEntry("Executable", m_Executable);
  268. m_pConfig->writePathEntry("Command", m_Command);
  269. m_pConfig->writeEntry("PreviewCommand", m_PreviewCommand);
  270. m_pConfig->writeEntry("Refresh", m_Refresh);
  271. m_pConfig->sync();
  272. dirty = false;
  273. }
  274. bool KBackgroundProgram::isAvailable()
  275. {
  276. return !m_pDirs->findExe(m_Executable).isEmpty();
  277. }
  278. bool KBackgroundProgram::remove()
  279. {
  280. if (m_bReadOnly)
  281. return false;
  282. return !unlink(TQFile::encodeName(m_File));
  283. }
  284. bool KBackgroundProgram::needUpdate()
  285. {
  286. return (m_LastChange + 60*m_Refresh <= time(0L));
  287. }
  288. void KBackgroundProgram::update()
  289. {
  290. m_LastChange = (int) time(0L);
  291. }
  292. TQString KBackgroundProgram::fingerprint()
  293. {
  294. return TQString("co:%1;re:%2").arg(m_Command).arg(m_Refresh);
  295. }
  296. int KBackgroundProgram::hash()
  297. {
  298. if (hashdirty) {
  299. m_Hash = TQHash(fingerprint());
  300. hashdirty = false;
  301. }
  302. return m_Hash;
  303. }
  304. /* static */
  305. TQStringList KBackgroundProgram::list()
  306. {
  307. TDEStandardDirs *dirs = TDEGlobal::dirs();
  308. dirs->addResourceType("dtop_program", dirs->kde_default("data") +
  309. "kdesktop/programs");
  310. TQStringList lst = dirs->findAllResources("dtop_program", "*.desktop",
  311. false, true);
  312. TQStringList::Iterator it;
  313. for (it=lst.begin(); it!=lst.end(); ++it) {
  314. // Strip path and suffix
  315. int pos = (*it).findRev('/');
  316. if (pos != -1)
  317. (*it) = (*it).mid(pos+1);
  318. pos = (*it).findRev('.');
  319. if (pos != -1)
  320. (*it) = (*it).left(pos);
  321. }
  322. return lst;
  323. }
  324. /**** KBackgroundSettings ****/
  325. KBackgroundSettings::KBackgroundSettings(int desk, int screen, bool drawBackgroundPerScreen, TDEConfig *config)
  326. : KBackgroundPattern(),
  327. KBackgroundProgram()
  328. {
  329. dirty = false; hashdirty = true;
  330. m_bDrawBackgroundPerScreen = drawBackgroundPerScreen;
  331. m_Desk = desk;
  332. m_Screen = screen;
  333. m_bEnabled = true;
  334. // Default values.
  335. defColorA = _defColorA;
  336. defColorB = _defColorB;
  337. if (TQPixmap::defaultDepth() > 8)
  338. defBackgroundMode = _defBackgroundMode;
  339. else
  340. defBackgroundMode = Flat;
  341. defWallpaperMode = _defWallpaperMode;
  342. defMultiMode = _defMultiMode;
  343. defBlendMode = _defBlendMode;
  344. defBlendBalance = _defBlendBalance;
  345. defReverseBlending = _defReverseBlending;
  346. defCrossFadeBg = _defCrossFadeBg;
  347. m_MinOptimizationDepth = _defMinOptimizationDepth;
  348. m_bShm = _defShm;
  349. // Background modes
  350. #define ADD_STRING(ID) m_BMMap[#ID] = ID; m_BMRevMap[ID] = (char *) #ID;
  351. ADD_STRING(Flat)
  352. ADD_STRING(Pattern)
  353. ADD_STRING(Program)
  354. ADD_STRING(HorizontalGradient)
  355. ADD_STRING(VerticalGradient)
  356. ADD_STRING(PyramidGradient)
  357. ADD_STRING(PipeCrossGradient)
  358. ADD_STRING(EllipticGradient)
  359. #undef ADD_STRING
  360. // Blend modes
  361. #define ADD_STRING(ID) m_BlMMap[#ID] = ID; m_BlMRevMap[ID] = (char *) #ID;
  362. ADD_STRING(NoBlending)
  363. ADD_STRING(FlatBlending)
  364. ADD_STRING(HorizontalBlending)
  365. ADD_STRING(VerticalBlending)
  366. ADD_STRING(PyramidBlending)
  367. ADD_STRING(PipeCrossBlending)
  368. ADD_STRING(EllipticBlending)
  369. ADD_STRING(IntensityBlending)
  370. ADD_STRING(SaturateBlending)
  371. ADD_STRING(ContrastBlending)
  372. ADD_STRING(HueShiftBlending)
  373. #undef ADD_STRING
  374. // Wallpaper modes
  375. #define ADD_STRING(ID) m_WMMap[#ID] = ID; m_WMRevMap[ID] = (char *) #ID;
  376. ADD_STRING(NoWallpaper)
  377. ADD_STRING(Centred)
  378. ADD_STRING(Tiled)
  379. ADD_STRING(CenterTiled)
  380. ADD_STRING(CentredMaxpect)
  381. ADD_STRING(TiledMaxpect)
  382. ADD_STRING(Scaled)
  383. ADD_STRING(CentredAutoFit)
  384. ADD_STRING(ScaleAndCrop)
  385. #undef ADD_STRING
  386. // Multiple wallpaper modes
  387. #define ADD_STRING(ID) m_MMMap[#ID] = ID; m_MMRevMap[ID] = (char *) #ID;
  388. ADD_STRING(NoMulti)
  389. ADD_STRING(InOrder)
  390. ADD_STRING(Random)
  391. ADD_STRING(NoMultiRandom)
  392. #undef ADD_STRING
  393. m_pDirs = TDEGlobal::dirs();
  394. if (!config) {
  395. int screen_number = 0;
  396. if (tqt_xdisplay())
  397. screen_number = DefaultScreen(tqt_xdisplay());
  398. TQCString configname;
  399. if (screen_number == 0)
  400. configname = "kdesktoprc";
  401. else
  402. configname.sprintf("kdesktop-screen-%drc", screen_number);
  403. m_pConfig = new TDEConfig(configname, false, false);
  404. m_bDeleteConfig = true;
  405. } else {
  406. m_pConfig = config;
  407. m_bDeleteConfig = false;
  408. }
  409. if (m_Desk == -1)
  410. return;
  411. readSettings();
  412. }
  413. KBackgroundSettings::~KBackgroundSettings()
  414. {
  415. if (m_bDeleteConfig)
  416. delete m_pConfig;
  417. }
  418. void KBackgroundSettings::copyConfig(const KBackgroundSettings *settings)
  419. {
  420. dirty = true;
  421. hashdirty = true;
  422. m_ColorA = settings->m_ColorA;
  423. m_ColorB = settings->m_ColorB;
  424. m_Wallpaper = settings->m_Wallpaper;
  425. m_WallpaperList = settings->m_WallpaperList;
  426. m_WallpaperFiles = settings->m_WallpaperFiles;
  427. m_BackgroundMode = settings->m_BackgroundMode;
  428. m_WallpaperMode = settings->m_WallpaperMode;
  429. m_BlendMode = settings->m_BlendMode;
  430. m_BlendBalance = settings->m_BlendBalance;
  431. m_ReverseBlending = settings->m_ReverseBlending;
  432. m_CrossFadeBg = settings->m_CrossFadeBg;
  433. m_MinOptimizationDepth = settings->m_MinOptimizationDepth;
  434. m_bShm = settings->m_bShm;
  435. m_MultiMode = settings->m_MultiMode;
  436. m_Interval = settings->m_Interval;
  437. m_CurrentWallpaper = settings->m_CurrentWallpaper;
  438. m_CurrentWallpaperName = settings->m_CurrentWallpaperName;
  439. KBackgroundPattern::copyConfig(settings);
  440. KBackgroundProgram::copyConfig(settings);
  441. }
  442. void KBackgroundSettings::load(int desk, int screen, bool drawBackgroundPerScreen, bool reparseConfig)
  443. {
  444. m_Desk = desk;
  445. m_Screen = screen;
  446. m_bDrawBackgroundPerScreen = drawBackgroundPerScreen;
  447. readSettings(reparseConfig);
  448. }
  449. void KBackgroundSettings::setColorA(const TQColor& color)
  450. {
  451. if (m_ColorA == color)
  452. return;
  453. dirty = hashdirty = true;
  454. m_ColorA = color;
  455. }
  456. void KBackgroundSettings::setColorB(const TQColor& color)
  457. {
  458. if (m_ColorB == color)
  459. return;
  460. dirty = hashdirty = true;
  461. m_ColorB = color;
  462. }
  463. void KBackgroundSettings::setPatternName(TQString name)
  464. {
  465. int ohash = KBackgroundPattern::hash();
  466. KBackgroundPattern::load(name);
  467. if (ohash == KBackgroundPattern::hash())
  468. return;
  469. dirty = hashdirty = true;
  470. return;
  471. }
  472. void KBackgroundSettings::setProgram(TQString name)
  473. {
  474. int ohash = KBackgroundProgram::hash();
  475. KBackgroundProgram::load(name);
  476. if (ohash == KBackgroundProgram::hash())
  477. return;
  478. dirty = hashdirty = true;
  479. return;
  480. }
  481. void KBackgroundSettings::setBackgroundMode(int mode)
  482. {
  483. if (m_BackgroundMode == mode)
  484. return;
  485. dirty = hashdirty = true;
  486. m_BackgroundMode = mode;
  487. }
  488. void KBackgroundSettings::setBlendMode(int mode)
  489. {
  490. if (m_BlendMode == mode)
  491. return;
  492. dirty = hashdirty = true;
  493. m_BlendMode = mode;
  494. }
  495. void KBackgroundSettings::setBlendBalance(int value)
  496. {
  497. if (m_BlendBalance == value)
  498. return;
  499. dirty = hashdirty = true;
  500. m_BlendBalance = value;
  501. }
  502. void KBackgroundSettings::setReverseBlending(bool value)
  503. {
  504. if (m_ReverseBlending == value)
  505. return;
  506. dirty = hashdirty = true;
  507. m_ReverseBlending = value;
  508. }
  509. void KBackgroundSettings::setCrossFadeBg(bool value)
  510. {
  511. if (m_CrossFadeBg == value)
  512. return;
  513. dirty = hashdirty = true;
  514. m_CrossFadeBg = value;
  515. }
  516. void KBackgroundSettings::setWallpaper(TQString wallpaper)
  517. {
  518. dirty = hashdirty = true;
  519. m_Wallpaper = wallpaper;
  520. }
  521. void KBackgroundSettings::setWallpaperMode(int mode)
  522. {
  523. if (m_WallpaperMode == mode)
  524. return;
  525. dirty = hashdirty = true;
  526. m_WallpaperMode = mode;
  527. }
  528. void KBackgroundSettings::setWallpaperList(TQStringList list)
  529. {
  530. TDEStandardDirs *d = TDEGlobal::dirs();
  531. if (m_WallpaperList == list)
  532. return;
  533. dirty = hashdirty = true;
  534. m_WallpaperList.clear();
  535. for(TQStringList::ConstIterator it = list.begin();
  536. it != list.end(); ++it)
  537. {
  538. TQString rpath = d->relativeLocation("wallpaper", *it);
  539. m_WallpaperList.append( !rpath.isEmpty() ? rpath : *it );
  540. }
  541. updateWallpaperFiles();
  542. // Try to keep the current wallpaper (-1 to set position to one before it)
  543. m_CurrentWallpaper = m_WallpaperFiles.findIndex(m_CurrentWallpaperName) - 1;
  544. changeWallpaper(m_CurrentWallpaper < 0);
  545. }
  546. void KBackgroundSettings::setWallpaperChangeInterval(int interval)
  547. {
  548. if (m_Interval == interval)
  549. return;
  550. dirty = hashdirty = true;
  551. m_Interval = interval;
  552. }
  553. void KBackgroundSettings::setMultiWallpaperMode(int mode)
  554. {
  555. if (m_MultiMode == mode)
  556. return;
  557. dirty = hashdirty = true;
  558. m_MultiMode = mode;
  559. changeWallpaper(true);
  560. }
  561. void KBackgroundSettings::setMinOptimizationDepth(int mode)
  562. {
  563. if (m_MinOptimizationDepth == mode)
  564. return;
  565. dirty = hashdirty = true;
  566. m_MinOptimizationDepth = mode;
  567. }
  568. bool KBackgroundSettings::optimize() const
  569. {
  570. switch( m_MinOptimizationDepth )
  571. {
  572. case AlwaysOpt :
  573. return true;
  574. case Opt16bpp :
  575. return TQPixmap::defaultDepth() >= 16;
  576. case Opt15bpp :
  577. return TQPixmap::defaultDepth() >= 15;
  578. case NeverOpt :
  579. default :
  580. return false;
  581. }
  582. }
  583. void KBackgroundSettings::setUseShm(bool use)
  584. {
  585. if (m_bShm == use)
  586. return;
  587. dirty = hashdirty = true;
  588. m_bShm = use;
  589. }
  590. TQString KBackgroundSettings::configGroupName() const
  591. {
  592. TQString screenName;
  593. if (m_bDrawBackgroundPerScreen)
  594. screenName = TQString("Screen%1").arg(TQString::number(m_Screen));
  595. return TQString("Desktop%1%2").arg(m_Desk).arg(screenName);
  596. }
  597. void KBackgroundSettings::readSettings(bool reparse)
  598. {
  599. if (reparse)
  600. m_pConfig->reparseConfiguration();
  601. m_pConfig->setGroup(configGroupName());
  602. // Background mode (Flat, div. Gradients, Pattern or Program)
  603. m_ColorA = m_pConfig->readColorEntry("Color1", &defColorA);
  604. m_ColorB = m_pConfig->readColorEntry("Color2", &defColorB);
  605. TQString s = m_pConfig->readPathEntry("Pattern");
  606. if (!s.isEmpty())
  607. KBackgroundPattern::load(s);
  608. s = m_pConfig->readPathEntry("Program");
  609. if (!s.isEmpty())
  610. KBackgroundProgram::load(s);
  611. m_BackgroundMode = defBackgroundMode;
  612. s = m_pConfig->readEntry("BackgroundMode", "invalid");
  613. if (m_BMMap.contains(s)) {
  614. int mode = m_BMMap[s];
  615. // consistency check
  616. if ( ((mode != Pattern) && (mode != Program)) ||
  617. ((mode == Pattern) && !pattern().isEmpty()) ||
  618. ((mode == Program) && !command().isEmpty())
  619. )
  620. m_BackgroundMode = mode;
  621. }
  622. m_BlendMode = defBlendMode;
  623. s = m_pConfig->readEntry("BlendMode", "invalid");
  624. if (m_BlMMap.contains(s)) {
  625. m_BlendMode = m_BlMMap[s];
  626. }
  627. m_BlendBalance = defBlendBalance;
  628. int value = m_pConfig->readNumEntry( "BlendBalance", defBlendBalance);
  629. if (value > -201 && value < 201)
  630. m_BlendBalance = value;
  631. m_ReverseBlending = m_pConfig->readBoolEntry( "ReverseBlending", defReverseBlending);
  632. m_CrossFadeBg = m_pConfig->readBoolEntry( "CrossFadeBg", defCrossFadeBg);
  633. // Multiple wallpaper config
  634. m_WallpaperList = m_pConfig->readPathListEntry("WallpaperList");
  635. m_Interval = m_pConfig->readNumEntry("ChangeInterval", 60);
  636. m_LastChange = m_pConfig->readNumEntry("LastChange", 0);
  637. m_CurrentWallpaper = m_pConfig->readNumEntry("CurrentWallpaper", 0);
  638. m_CurrentWallpaperName = m_pConfig->readEntry("CurrentWallpaperName");
  639. m_MultiMode = defMultiMode;
  640. s = m_pConfig->readEntry("MultiWallpaperMode");
  641. if (m_MMMap.contains(s)) {
  642. int mode = m_MMMap[s];
  643. m_MultiMode = mode;
  644. }
  645. updateWallpaperFiles();
  646. if( !m_CurrentWallpaperName.isEmpty())
  647. m_CurrentWallpaper = m_WallpaperFiles.findIndex(m_CurrentWallpaperName);
  648. if(m_CurrentWallpaper < 0)
  649. m_CurrentWallpaper = 0;
  650. // Wallpaper mode (NoWallpaper, div. tilings)
  651. m_WallpaperMode = defWallpaperMode;
  652. m_Wallpaper = m_pConfig->readPathEntry("Wallpaper");
  653. s = m_pConfig->readEntry("WallpaperMode", "invalid");
  654. if (m_WMMap.contains(s)) {
  655. int mode = m_WMMap[s];
  656. // consistency check.
  657. if ((mode == NoWallpaper) || !m_Wallpaper.isEmpty() || (m_MultiMode == InOrder || m_MultiMode == Random))
  658. m_WallpaperMode = mode;
  659. }
  660. m_MinOptimizationDepth = m_pConfig->readNumEntry( "MinOptimizationDepth",
  661. _defMinOptimizationDepth );
  662. m_bShm = m_pConfig->readBoolEntry( "UseSHM", _defShm );
  663. dirty = reparse; hashdirty = true;
  664. }
  665. void KBackgroundSettings::writeSettings()
  666. {
  667. KBackgroundPattern::writeSettings();
  668. KBackgroundProgram::writeSettings();
  669. if (!dirty)
  670. return;
  671. m_pConfig->setGroup(configGroupName());
  672. m_pConfig->writeEntry("Color1", m_ColorA);
  673. m_pConfig->writeEntry("Color2", m_ColorB);
  674. m_pConfig->writePathEntry("Pattern", KBackgroundPattern::name());
  675. m_pConfig->writeEntry("Program", KBackgroundProgram::name());
  676. m_pConfig->writeEntry("BackgroundMode", m_BMRevMap[m_BackgroundMode]);
  677. m_pConfig->writePathEntry("Wallpaper", m_Wallpaper);
  678. m_pConfig->writeEntry("WallpaperMode", m_WMRevMap[m_WallpaperMode]);
  679. m_pConfig->writeEntry("MultiWallpaperMode", m_MMRevMap[m_MultiMode]);
  680. m_pConfig->writeEntry("BlendMode", m_BlMRevMap[m_BlendMode]);
  681. m_pConfig->writeEntry("BlendBalance", m_BlendBalance);
  682. m_pConfig->writeEntry("ReverseBlending", m_ReverseBlending);
  683. m_pConfig->writeEntry("CrossFadeBg", m_CrossFadeBg);
  684. m_pConfig->writeEntry("MinOptimizationDepth", m_MinOptimizationDepth);
  685. m_pConfig->writeEntry("UseSHM", m_bShm);
  686. m_pConfig->writePathEntry("WallpaperList", m_WallpaperList);
  687. m_pConfig->writeEntry("ChangeInterval", m_Interval);
  688. m_pConfig->writeEntry("LastChange", m_LastChange);
  689. m_pConfig->deleteEntry("CurrentWallpaper"); // obsolete, remember name
  690. m_pConfig->writeEntry("CurrentWallpaperName", m_CurrentWallpaperName);
  691. m_pConfig->sync();
  692. dirty = false;
  693. }
  694. /*
  695. * (re)Build m_WallpaperFiles from m_WallpaperList
  696. */
  697. void KBackgroundSettings::updateWallpaperFiles()
  698. {
  699. TQStringList::Iterator it;
  700. m_WallpaperFiles.clear();
  701. for (it=m_WallpaperList.begin(); it!=m_WallpaperList.end(); ++it) {
  702. TQString file = locate("wallpaper", *it);
  703. if (file.isEmpty())
  704. continue;
  705. TQFileInfo fi(file);
  706. if (!fi.exists())
  707. continue;
  708. if (fi.isFile() && fi.isReadable())
  709. m_WallpaperFiles.append(file);
  710. if (fi.isDir()) {
  711. TQDir dir(file);
  712. TQStringList lst = dir.entryList(TQDir::Files | TQDir::Readable);
  713. TQStringList::Iterator it;
  714. for (it=lst.begin(); it!=lst.end(); ++it)
  715. {
  716. file = dir.absFilePath(*it);
  717. TQFileInfo fi(file);
  718. if (fi.isFile() && fi.isReadable())
  719. m_WallpaperFiles.append(file);
  720. }
  721. }
  722. }
  723. if (m_MultiMode == Random)
  724. randomizeWallpaperFiles();
  725. }
  726. // Randomize the m_WallpaperFiles in a non-repeating method.
  727. void KBackgroundSettings::randomizeWallpaperFiles()
  728. {
  729. if (m_WallpaperFiles.count() < 4)
  730. return;
  731. KRandomSequence rseq;
  732. TQStringList tmpList = m_WallpaperFiles;
  733. TQStringList randomList;
  734. randomList.append(tmpList.front());
  735. tmpList.pop_front();
  736. while(tmpList.count())
  737. {
  738. randomList.insert(randomList.at(
  739. rseq.getLong(randomList.count()+1)),
  740. 1, tmpList.front());
  741. tmpList.pop_front();
  742. }
  743. m_WallpaperFiles = randomList;
  744. }
  745. TQStringList KBackgroundSettings::wallpaperList() const
  746. {
  747. if ( m_WallpaperMode == NoWallpaper )
  748. return TQStringList();
  749. if ( m_MultiMode == NoMulti || m_MultiMode == NoMultiRandom )
  750. return TQStringList(m_Wallpaper);
  751. return m_WallpaperList;
  752. }
  753. TQStringList KBackgroundSettings::wallpaperFiles() const
  754. {
  755. if ( m_WallpaperMode == NoWallpaper )
  756. return TQStringList();
  757. if ( m_MultiMode == NoMulti || m_MultiMode == NoMultiRandom )
  758. return TQStringList(m_Wallpaper);
  759. return m_WallpaperFiles;
  760. }
  761. /*
  762. * Select a new wallpaper from the list.
  763. */
  764. void KBackgroundSettings::changeWallpaper(bool init)
  765. {
  766. if (m_WallpaperFiles.count() == 0) {
  767. if( init ) {
  768. m_CurrentWallpaper = 0;
  769. m_CurrentWallpaperName = TQString();
  770. }
  771. return;
  772. }
  773. switch (m_MultiMode) {
  774. case InOrder:
  775. m_CurrentWallpaper++;
  776. if (init || (m_CurrentWallpaper >= (int) m_WallpaperFiles.count()))
  777. m_CurrentWallpaper = 0;
  778. break;
  779. case Random:
  780. // Random: m_WallpaperFiles is randomized in a non-repeating
  781. // method. Hence we just increment the index.
  782. m_CurrentWallpaper++;
  783. if (init || (m_CurrentWallpaper >= (int) m_WallpaperFiles.count())) {
  784. m_CurrentWallpaper = 0;
  785. randomizeWallpaperFiles(); // Get a new random-ordered list.
  786. }
  787. break;
  788. default:
  789. break;
  790. }
  791. m_CurrentWallpaperName = m_WallpaperFiles[ m_CurrentWallpaper ];
  792. m_LastChange = (int) time(0L);
  793. m_pConfig->setGroup(configGroupName());
  794. m_pConfig->deleteEntry("CurrentWallpaper"); // obsolete, remember name
  795. m_pConfig->writeEntry("CurrentWallpaperName", m_CurrentWallpaperName);
  796. m_pConfig->writeEntry("LastChange", m_LastChange);
  797. m_pConfig->sync();
  798. hashdirty = true;
  799. }
  800. TQString KBackgroundSettings::currentWallpaper() const
  801. {
  802. if ( m_WallpaperMode == NoWallpaper )
  803. return TQString::null;
  804. if (m_MultiMode == NoMulti || m_MultiMode == NoMultiRandom)
  805. return m_Wallpaper;
  806. if (m_CurrentWallpaper >= 0 && m_CurrentWallpaper < (int) m_WallpaperFiles.count())
  807. return m_WallpaperFiles[m_CurrentWallpaper];
  808. return TQString::null;
  809. }
  810. bool KBackgroundSettings::discardCurrentWallpaper()
  811. {
  812. if (m_MultiMode == NoMulti || m_MultiMode == NoMultiRandom)
  813. {
  814. return false;
  815. }
  816. m_WallpaperFiles.remove(m_WallpaperFiles.at(m_CurrentWallpaper));
  817. --m_CurrentWallpaper;
  818. changeWallpaper();
  819. return true;
  820. }
  821. bool KBackgroundSettings::needWallpaperChange()
  822. {
  823. if (m_MultiMode == NoMulti || m_MultiMode == NoMultiRandom)
  824. return false;
  825. return ((m_LastChange + 60*m_Interval) <= time(0L));
  826. }
  827. /*
  828. * Create a fingerprint string for this config. Be somewhat (overly) carefull
  829. * that only a different final result will give a different fingerprint.
  830. */
  831. TQString KBackgroundSettings::fingerprint()
  832. {
  833. TQString s = TQString("bm:%1;en:%2").arg(m_BackgroundMode).arg(m_bEnabled);
  834. switch (m_BackgroundMode) {
  835. case Flat:
  836. s += TQString("ca:%1;").arg(m_ColorA.rgb());
  837. break;
  838. case Program:
  839. s += TQString("pr:%1;").arg(KBackgroundProgram::hash());
  840. break;
  841. case Pattern:
  842. s += TQString("ca:%1;cb:%2;pt:%3;").arg(m_ColorA.rgb())
  843. .arg(m_ColorB.rgb()).arg(KBackgroundPattern::hash());
  844. break;
  845. default:
  846. s += TQString("ca:%1;cb:%2;").arg(m_ColorA.rgb()).arg(m_ColorB.rgb());
  847. break;
  848. }
  849. s += TQString("wm:%1;").arg(m_WallpaperMode);
  850. if (m_WallpaperMode != NoWallpaper)
  851. {
  852. TQ_UINT32 rh = TDEGlobal::dirs()->calcResourceHash("wallpaper", currentWallpaper(), false);
  853. s += TQString("wp:%2:%1;").arg(rh).arg(currentWallpaper());
  854. }
  855. s += TQString("blm:%1;").arg(m_BlendMode);
  856. if (m_BlendMode != NoBlending) {
  857. s += TQString("blb:%1;").arg(m_BlendBalance);
  858. s += TQString("rbl:%1;").arg(int(m_ReverseBlending));
  859. }
  860. s += TQString::number( m_bShm );
  861. s += TQString::number( m_MinOptimizationDepth );
  862. return s;
  863. }
  864. int KBackgroundSettings::hash()
  865. {
  866. if (hashdirty) {
  867. m_Hash = TQHash(fingerprint());
  868. hashdirty = false;
  869. }
  870. return m_Hash;
  871. }
  872. void KBackgroundSettings::setEnabled(const bool enable)
  873. {
  874. if (m_bEnabled == enable)
  875. return;
  876. m_bEnabled= enable;
  877. hashdirty = true;
  878. }
  879. /**** TDEGlobalBackgroundSettings ****/
  880. TDEGlobalBackgroundSettings::TDEGlobalBackgroundSettings(TDEConfig *_config)
  881. {
  882. m_pConfig = _config;
  883. readSettings();
  884. }
  885. TQString TDEGlobalBackgroundSettings::deskName(int desk)
  886. {
  887. return m_Names[desk];
  888. }
  889. /*
  890. void TDEGlobalBackgroundSettings::setDeskName(int desk, TQString name)
  891. {
  892. if (name == m_Names[desk])
  893. return;
  894. dirty = true;
  895. m_Names[desk] = name;
  896. }
  897. */
  898. void TDEGlobalBackgroundSettings::setCacheSize(int size)
  899. {
  900. if (size == m_CacheSize)
  901. return;
  902. dirty = true;
  903. m_CacheSize = size;
  904. }
  905. void TDEGlobalBackgroundSettings::setLimitCache(bool limit)
  906. {
  907. if (limit == m_bLimitCache)
  908. return;
  909. dirty = true;
  910. m_bLimitCache = limit;
  911. }
  912. bool TDEGlobalBackgroundSettings::drawBackgroundPerScreen(int desk) const
  913. {
  914. if ( desk > int(m_bDrawBackgroundPerScreen.size()) )
  915. return _defDrawBackgroundPerScreen;
  916. return m_bDrawBackgroundPerScreen[desk];
  917. }
  918. void TDEGlobalBackgroundSettings::setDrawBackgroundPerScreen(int desk, bool perScreen)
  919. {
  920. if ( desk >= int(m_bDrawBackgroundPerScreen.size()) )
  921. return;
  922. if ( m_bDrawBackgroundPerScreen[desk] == perScreen )
  923. return;
  924. dirty = true;
  925. m_bDrawBackgroundPerScreen[desk] = perScreen;
  926. }
  927. void TDEGlobalBackgroundSettings::setCommonScreenBackground(bool common)
  928. {
  929. if (common == m_bCommonScreen)
  930. return;
  931. dirty = true;
  932. m_bCommonScreen = common;
  933. }
  934. void TDEGlobalBackgroundSettings::setCommonDeskBackground(bool common)
  935. {
  936. if (common == m_bCommonDesk)
  937. return;
  938. dirty = true;
  939. m_bCommonDesk = common;
  940. }
  941. void TDEGlobalBackgroundSettings::setDockPanel(bool dock)
  942. {
  943. if (dock == m_bDock)
  944. return;
  945. dirty = true;
  946. m_bDock = dock;
  947. }
  948. void TDEGlobalBackgroundSettings::setExportBackground(bool _export)
  949. {
  950. if (_export == m_bExport)
  951. return;
  952. dirty = true;
  953. m_bExport = _export;
  954. }
  955. void TDEGlobalBackgroundSettings::setTextColor(TQColor _color)
  956. {
  957. if (_color == m_TextColor)
  958. return;
  959. dirty = true;
  960. m_TextColor = _color;
  961. }
  962. void TDEGlobalBackgroundSettings::setTextBackgroundColor(TQColor _color)
  963. {
  964. if (_color == m_TextBackgroundColor)
  965. return;
  966. dirty = true;
  967. m_TextBackgroundColor = _color;
  968. }
  969. void TDEGlobalBackgroundSettings::setShadowEnabled(bool enabled)
  970. {
  971. if (enabled == m_shadowEnabled)
  972. return;
  973. dirty = true;
  974. m_shadowEnabled = enabled;
  975. }
  976. void TDEGlobalBackgroundSettings::setTextLines(int lines)
  977. {
  978. if (lines == m_textLines)
  979. return;
  980. dirty = true;
  981. m_textLines = lines;
  982. }
  983. void TDEGlobalBackgroundSettings::setTextWidth(int width)
  984. {
  985. if (width == m_textWidth)
  986. return;
  987. dirty = true;
  988. m_textWidth = width;
  989. }
  990. void TDEGlobalBackgroundSettings::readSettings()
  991. {
  992. m_pConfig->setGroup("Background Common");
  993. m_bCommonScreen = m_pConfig->readBoolEntry("CommonScreen", _defCommonScreen);
  994. m_bCommonDesk = m_pConfig->readBoolEntry("CommonDesktop", _defCommonDesk);
  995. m_bDock = m_pConfig->readBoolEntry("Dock", _defDock);
  996. m_bExport = m_pConfig->readBoolEntry("Export", _defExport);
  997. m_bLimitCache = m_pConfig->readBoolEntry("LimitCache", _defLimitCache);
  998. m_CacheSize = m_pConfig->readNumEntry("CacheSize", _defCacheSize);
  999. m_Names.clear();
  1000. NETRootInfo info( tqt_xdisplay(), NET::DesktopNames | NET::NumberOfDesktops );
  1001. m_bDrawBackgroundPerScreen.resize(info.numberOfDesktops());
  1002. for ( int i = 0 ; i < info.numberOfDesktops() ; ++i )
  1003. m_bDrawBackgroundPerScreen[i] = m_pConfig->readBoolEntry( TQString("DrawBackgroundPerScreen_%1").arg(i), _defDrawBackgroundPerScreen );
  1004. m_TextColor = TDEGlobalSettings::textColor();
  1005. m_pConfig->setGroup("FMSettings");
  1006. m_TextColor = m_pConfig->readColorEntry("NormalTextColor", &m_TextColor);
  1007. m_TextBackgroundColor = m_pConfig->readColorEntry("ItemTextBackground");
  1008. m_shadowEnabled = m_pConfig->readBoolEntry("ShadowEnabled", true);
  1009. m_textLines = m_pConfig->readNumEntry("TextHeight", DEFAULT_TEXTHEIGHT);
  1010. m_textWidth = m_pConfig->readNumEntry("TextWidth", DEFAULT_TEXTWIDTH);
  1011. for ( int i = 0 ; i < info.numberOfDesktops() ; ++i )
  1012. m_Names.append( TQString::fromUtf8(info.desktopName(i+1)) );
  1013. dirty = false;
  1014. }
  1015. void TDEGlobalBackgroundSettings::writeSettings()
  1016. {
  1017. if (!dirty)
  1018. return;
  1019. m_pConfig->setGroup("Background Common");
  1020. m_pConfig->writeEntry("CommonScreen", m_bCommonScreen);
  1021. m_pConfig->writeEntry("CommonDesktop", m_bCommonDesk);
  1022. m_pConfig->writeEntry("Dock", m_bDock);
  1023. m_pConfig->writeEntry("Export", m_bExport);
  1024. m_pConfig->writeEntry("LimitCache", m_bLimitCache);
  1025. m_pConfig->writeEntry("CacheSize", m_CacheSize);
  1026. for ( unsigned i = 0 ; i < m_bDrawBackgroundPerScreen.size() ; ++i )
  1027. m_pConfig->writeEntry(TQString("DrawBackgroundPerScreen_%1").arg(i), m_bDrawBackgroundPerScreen[i] );
  1028. m_pConfig->setGroup("FMSettings");
  1029. m_pConfig->writeEntry("NormalTextColor", m_TextColor);
  1030. m_pConfig->writeEntry("ItemTextBackground", m_TextBackgroundColor);
  1031. m_pConfig->writeEntry("ShadowEnabled", m_shadowEnabled);
  1032. m_pConfig->writeEntry("TextHeight", m_textLines);
  1033. m_pConfig->writeEntry("TextWidth", m_textWidth);
  1034. m_pConfig->sync();
  1035. dirty = false;
  1036. // tell kdesktop to get it's butt in gear and pick up the new settings
  1037. TQByteArray data;
  1038. kapp->dcopClient()->send("kdesktop", "KDesktopIface", "configure()", data);
  1039. }