TDE personal information management applications
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.

boxcontaineritem.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright (C) 2004, Mart Kelder (mart.kde@hccnet.nl)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "boxcontaineritem.h"
  19. #include "mailsubject.h"
  20. #include <tdeaboutapplication.h>
  21. #include <tdeactioncollection.h>
  22. #include <tdeapplication.h>
  23. #include <kbugreport.h>
  24. #include <tdeconfig.h>
  25. #include <kdebug.h>
  26. #include <tdeglobal.h>
  27. #include <kiconeffect.h>
  28. #include <kiconloader.h>
  29. #include <tdelocale.h>
  30. #include <kpassivepopup.h>
  31. #include <tdepopupmenu.h>
  32. #include <kprocess.h>
  33. #include <tdeshortcut.h>
  34. #include <tqbitmap.h>
  35. #include <tqcolor.h>
  36. #include <tqfont.h>
  37. #include <tqgrid.h>
  38. #include <tqlabel.h>
  39. #include <tqpainter.h>
  40. #include <tqpixmap.h>
  41. #include <tqptrlist.h>
  42. #include <tqstring.h>
  43. #include <tqtooltip.h>
  44. #include <tqvbox.h>
  45. BoxContainerItem::BoxContainerItem( TQObject * parent, const char * name )
  46. : AccountManager( parent, name ),
  47. DCOPObject(),
  48. _command( new TQString )
  49. {
  50. short i;
  51. for( i = 0; i < 2; ++i )
  52. {
  53. _icons[ i ] = 0;
  54. _anims[ i ] = 0;
  55. _fgColour[ i ] = 0;
  56. _bgColour[ i ] = 0;
  57. _fonts[ i ] = 0;
  58. }
  59. for( i = 0; i < 3; ++i )
  60. {
  61. _recheckSettings[ i ] = false;
  62. _resetSettings[ i ] = false;
  63. _viewSettings[ i ] = false;
  64. _runSettings[ i ] = false;
  65. _popupSettings[ i ] = false;
  66. }
  67. }
  68. BoxContainerItem::~BoxContainerItem()
  69. {
  70. delete _command;
  71. }
  72. void BoxContainerItem::readConfig( TDEConfig* config, const int index )
  73. {
  74. //Read information about how the thing have to look like
  75. config->setGroup( TQString( "korn-%1" ).arg( index ) );
  76. if( config->readBoolEntry( "hasnormalicon", false ) )
  77. _icons[ 0 ] = new TQString( config->readEntry( "normalicon", "" ) );
  78. else
  79. _icons[ 0 ] = 0;
  80. if( config->readBoolEntry( "hasnewicon", false ) )
  81. _icons[ 1 ] = new TQString( config->readEntry( "newicon", "" ) );
  82. else
  83. _icons[ 1 ] = 0;
  84. if( config->readBoolEntry( "hasnormalanim", false ) )
  85. _anims[ 0 ] = new TQString( config->readEntry( "normalanim", "" ) );
  86. else
  87. _anims[ 0 ] = 0;
  88. if( config->readBoolEntry( "hasnewanim", false ) )
  89. _anims[ 1 ] = new TQString( config->readEntry( "newanim", "" ) );
  90. else
  91. _anims[ 1 ] = 0;
  92. if( config->readBoolEntry( "hasnormalfgcolour", false ) )
  93. _fgColour[ 0 ] = new TQColor( config->readColorEntry( "normalfgcolour" ) );
  94. else
  95. _fgColour[ 0 ] = 0;
  96. if( config->readBoolEntry( "hasnewfgcolour", false ) )
  97. _fgColour[ 1 ] = new TQColor( config->readColorEntry( "newfgcolour" ) );
  98. else
  99. _fgColour[ 1 ] = 0;
  100. if( config->readBoolEntry( "hasnormalbgcolour", false ) )
  101. _bgColour[ 0 ] = new TQColor( config->readColorEntry( "normalbgcolour" ) );
  102. else
  103. _bgColour[ 0 ] = 0;
  104. if( config->readBoolEntry( "hasnewbgcolour", false ) )
  105. _bgColour[ 1 ] = new TQColor( config->readColorEntry( "newbgcolour" ) );
  106. else
  107. _bgColour[ 1 ] = 0;
  108. if( config->readBoolEntry( "hasnormalfont", false ) )
  109. _fonts[ 0 ] = new TQFont( config->readFontEntry( "normalfont" ) );
  110. else
  111. _fonts[ 0 ] = 0;
  112. if( config->readBoolEntry( "hasnewfont", false ) )
  113. _fonts[ 1 ] = new TQFont( config->readFontEntry( "newfont" ) );
  114. else
  115. _fonts[ 1 ] = 0;
  116. //Read information about the mappings.
  117. _recheckSettings[ 0 ] = config->readBoolEntry( "leftrecheck", true );
  118. _recheckSettings[ 1 ] = config->readBoolEntry( "middlerecheck", false );
  119. _recheckSettings[ 2 ] = config->readBoolEntry( "rightrecheck", false );
  120. _resetSettings[ 0 ] = config->readBoolEntry( "leftreset", false );
  121. _resetSettings[ 1 ] = config->readBoolEntry( "middlereset", false );
  122. _resetSettings[ 2 ] = config->readBoolEntry( "rightreset", false );
  123. _viewSettings[ 0 ] = config->readBoolEntry( "leftview", false );
  124. _viewSettings[ 1 ] = config->readBoolEntry( "middleview", false );
  125. _viewSettings[ 2 ] = config->readBoolEntry( "rightview", false );
  126. _runSettings[ 0 ] = config->readBoolEntry( "leftrun", false );
  127. _runSettings[ 1 ] = config->readBoolEntry( "middlerun", false );
  128. _runSettings[ 2 ] = config->readBoolEntry( "rightrun", false );
  129. _popupSettings[ 0 ] = config->readBoolEntry( "leftpopup", false );
  130. _popupSettings[ 1 ] = config->readBoolEntry( "middlepopup", false );
  131. _popupSettings[ 2 ] = config->readBoolEntry( "rightpopup", true );
  132. //Read the command
  133. *_command = config->readEntry( "command", "" );
  134. //Sets the object ID for the DCOP-object
  135. this->setObjId( config->readEntry( "name", "" ).utf8() );
  136. //Read the settings of the reimplemented class.
  137. //It is important to read this after the box-settings, because the
  138. //setCount-function is called in AccountManager::readConfig
  139. AccountManager::readConfig( config, index );
  140. }
  141. void BoxContainerItem::runCommand( const TQString& cmd )
  142. {
  143. TDEProcess *process = new TDEProcess;
  144. process->setUseShell( true );
  145. *process << cmd;
  146. connect( process, TQT_SIGNAL( processExited (TDEProcess *) ), this, TQT_SLOT( processExited( TDEProcess * ) ) );
  147. process->start();
  148. }
  149. void BoxContainerItem::mouseButtonPressed( TQt::ButtonState state )
  150. {
  151. int button;
  152. if( state & Qt::LeftButton )
  153. button = 0;
  154. else if( state & Qt::RightButton )
  155. button = 2;
  156. else if( state & Qt::MidButton )
  157. button = 1;
  158. else
  159. return; //Invalid mouse button
  160. if( _recheckSettings[ button ] )
  161. doRecheck();
  162. if( _resetSettings[ button ] )
  163. doReset();
  164. if( _viewSettings[ button ] )
  165. doView();
  166. if( _runSettings[ button ] )
  167. runCommand();
  168. if( _popupSettings[ button ] )
  169. doPopup();
  170. }
  171. void BoxContainerItem::fillTDEPopupMenu( TDEPopupMenu* popupMenu, TDEActionCollection* actions ) const
  172. {
  173. /*popupMenu->insertItem( i18n( "&Configure" ), this, TQT_SLOT( slotConfigure() ) );
  174. popupMenu->insertItem( i18n( "&Recheck" ), this, TQT_SLOT( slotRecheck() ) );
  175. popupMenu->insertItem( i18n( "R&eset Counter" ), this, TQT_SLOT( slotReset() ) );
  176. popupMenu->insertItem( i18n( "&View Emails" ), this, TQT_SLOT( slotView() ) );
  177. popupMenu->insertItem( i18n( "R&un Command" ), this, TQT_SLOT( slotRunCommand() ) );*/
  178. (new TDEAction( i18n("&Configure"), TDEShortcut(), this, TQT_SLOT( slotConfigure() ), actions ))->plug( popupMenu );
  179. (new TDEAction( i18n("&Recheck"), TDEShortcut(), this, TQT_SLOT( slotRecheck() ), actions ))->plug( popupMenu );
  180. (new TDEAction( i18n("R&eset Counter"), TDEShortcut(), this, TQT_SLOT( slotReset() ), actions ))->plug( popupMenu );
  181. (new TDEAction( i18n("&View Emails"), TDEShortcut(), this, TQT_SLOT( slotView() ), actions ))->plug( popupMenu );
  182. (new TDEAction( i18n("R&un Command"), TDEShortcut(), this, TQT_SLOT( slotRunCommand() ), actions ))->plug( popupMenu );
  183. popupMenu->insertSeparator();
  184. KStdAction::help( this, TQT_SLOT( help() ), actions )->plug( popupMenu );
  185. KStdAction::reportBug( this, TQT_SLOT( reportBug() ), actions )->plug( popupMenu );
  186. KStdAction::aboutApp( this, TQT_SLOT( about() ), actions )->plug( popupMenu );
  187. }
  188. void BoxContainerItem::showPassivePopup( TQWidget* parent, TQPtrList< KornMailSubject >* list, int total,
  189. const TQString &accountName, bool date )
  190. {
  191. KPassivePopup *popup = new KPassivePopup( parent, "Passive popup" );
  192. TQVBox *mainvlayout = popup->standardView( i18n( "KOrn - %1/%2 (total: %3)" ).arg( TQString(objId()) ).arg( accountName )
  193. .arg( total ), "", TQPixmap(), 0 );
  194. TQGrid *mainglayout = new TQGrid( date ? 3 : 2 ,mainvlayout, "Grid-Layout" );
  195. TQLabel *title = new TQLabel( i18n("From"), mainglayout, "from_label" );
  196. TQFont font = title->font();
  197. font.setBold( true );
  198. title->setFont( font );
  199. title = new TQLabel( i18n("Subject"), mainglayout, "subject_label" );
  200. font = title->font();
  201. font.setBold( true );
  202. title->setFont( font );
  203. if( date )
  204. {
  205. title = new TQLabel( i18n("Date"), mainglayout, "date_label" );
  206. font = title->font();
  207. font.setBold( true );
  208. title->setFont( font );
  209. }
  210. for( KornMailSubject* subject = list->first(); subject; subject = list->next() )
  211. {
  212. new TQLabel( subject->getSender(), mainglayout, "from-value" );
  213. new TQLabel( subject->getSubject(), mainglayout, "subject-value" );
  214. if( date )
  215. {
  216. TQDateTime tijd;
  217. tijd.setTime_t( subject->getDate() );
  218. new TQLabel( tijd.toString(), mainglayout, "date-value" );
  219. }
  220. }
  221. popup->setAutoDelete( true ); //Now, now care for deleting these pointers.
  222. popup->setView( mainvlayout );
  223. popup->show(); //Display it
  224. }
  225. void BoxContainerItem::drawLabel( TQLabel *label, const int count, const bool newMessages )
  226. {
  227. //This would fail if bool have fome other values.
  228. short index = newMessages ? 1 : 0;
  229. bool hasAnim = _anims[ index ] && !_anims[ index ]->isEmpty();
  230. bool hasIcon = _icons[ index ] && !_icons[ index ]->isEmpty();
  231. bool hasBg = _bgColour[ index ] && _bgColour[ index ]->isValid();
  232. bool hasFg = _fgColour[ index ] && _fgColour[ index ]->isValid();
  233. TQPixmap pixmap;
  234. label->setText( "" );
  235. //TQToolTip::add( label, this->getTooltip() );
  236. if( hasAnim )
  237. { //An animation can't have a foreground-colour and can't have a icon.
  238. setAnimIcon( label, *_anims[ index ] );
  239. hasFg = false;
  240. hasIcon = false;
  241. }
  242. if( hasIcon )
  243. pixmap = TDEGlobal::iconLoader()->loadIcon( *_icons[ index ], TDEIcon::Desktop, TDEIcon::SizeSmallMedium );
  244. if( hasIcon && hasFg )
  245. {
  246. if( hasBg )
  247. {
  248. label->setPixmap( calcComplexPixmap( pixmap, *_fgColour[ index ], _fonts[ index ], count ) );
  249. label->setBackgroundMode( TQt::FixedColor );
  250. label->setPaletteBackgroundColor( *_bgColour[ index ] );
  251. } else
  252. {
  253. label->setPixmap( calcComplexPixmap( pixmap, *_fgColour[ index ], _fonts[ index ], count ) );
  254. }
  255. return;
  256. }
  257. if( hasBg )
  258. {
  259. label->setBackgroundMode( TQt::FixedColor );
  260. label->setPaletteBackgroundColor( *_bgColour[ index ] );
  261. } else
  262. {
  263. label->setBackgroundMode( TQt::X11ParentRelative );
  264. }
  265. if( hasIcon )
  266. {
  267. label->setPixmap( pixmap );
  268. }
  269. if( hasFg )
  270. {
  271. if( _fonts[ index ] )
  272. label->setFont( *_fonts[ index ] );
  273. label->setPaletteForegroundColor( *_fgColour[ index ] );
  274. label->setAlignment( TQt::AlignHCenter | TQt::AlignVCenter );
  275. label->setText( TQString::number( count ) );
  276. }
  277. if( hasFg || hasBg || hasIcon || hasAnim )
  278. label->show();
  279. else
  280. label->hide();
  281. }
  282. //This function makes a pixmap which is based on icon, but has a number painted on it.
  283. TQPixmap BoxContainerItem::calcComplexPixmap( const TQPixmap &icon, const TQColor& fgColour, const TQFont* font, const int count )
  284. {
  285. TQPixmap result( icon );
  286. TQPixmap numberPixmap( icon.size() );
  287. TQImage iconImage( icon.convertToImage() );
  288. TQImage numberImage;
  289. TQRgb *rgbline;
  290. TQPainter p;
  291. //Make a transparent number; first make a white number on a black background.
  292. //This pixmap also is the base alpha-channel, the foreground colour is added later.
  293. numberPixmap.fill( TQt::black );
  294. p.begin( &numberPixmap, false );
  295. p.setPen( TQt::white );
  296. if( font )
  297. p.setFont( *font );
  298. p.drawText( icon.rect(), TQt::AlignCenter, TQString::number( count ) );
  299. p.end();
  300. //Convert to image and add the alpha channel.
  301. numberImage = numberPixmap.convertToImage();
  302. if( numberImage.depth() != 32 ) //Make sure depth is 32 (and thus can have an alpha channel)
  303. numberImage = numberImage.convertDepth( 32 );
  304. numberImage.setAlphaBuffer( true ); //Enable alpha channel
  305. for( int xx = 0; xx < numberImage.height(); ++xx )
  306. {
  307. rgbline = (TQRgb*)numberImage.scanLine( xx );
  308. for( int yy = 0; yy < numberImage.width(); ++yy )
  309. {
  310. //Set colour and alpha channel
  311. rgbline[ yy ] = tqRgba( fgColour.red(), fgColour.green(), fgColour.blue(), tqRed( rgbline[ yy ] ) );
  312. }
  313. }
  314. //Merge icon and number and convert to result.
  315. TDEIconEffect::overlay( iconImage, numberImage );
  316. result.convertFromImage( iconImage );
  317. return result;
  318. }
  319. void BoxContainerItem::setAnimIcon( TQLabel* label, const TQString& anim )
  320. {
  321. label->setAlignment( TQt::AlignHCenter | TQt::AlignVCenter );
  322. label->setMovie( TQMovie( anim ) );
  323. label->show();
  324. }
  325. void BoxContainerItem::recheck()
  326. {
  327. doRecheck();
  328. }
  329. void BoxContainerItem::reset()
  330. {
  331. doReset();
  332. }
  333. void BoxContainerItem::view()
  334. {
  335. doView();
  336. }
  337. void BoxContainerItem::runCommand()//Possible_unsafe?
  338. {
  339. if( _command->isEmpty() )
  340. return; //Don't execute an empty command
  341. runCommand( *_command );
  342. }
  343. void BoxContainerItem::help()
  344. {
  345. kapp->invokeHelp();
  346. }
  347. void BoxContainerItem::reportBug()
  348. {
  349. KBugReport bug( 0, true );
  350. bug.exec(); //modal: it doesn't recheck anymore
  351. }
  352. void BoxContainerItem::about()
  353. {
  354. TDEAboutApplication about( 0, "KOrn About", true );
  355. about.exec(); //modal: it doesn't recheck anymore
  356. }
  357. void BoxContainerItem::popup()
  358. {
  359. doPopup();
  360. }
  361. void BoxContainerItem::showConfig()
  362. {
  363. emit showConfiguration();
  364. }
  365. void BoxContainerItem::startTimer()
  366. {
  367. doStartTimer();
  368. }
  369. void BoxContainerItem::stopTimer()
  370. {
  371. doStopTimer();
  372. }
  373. void BoxContainerItem::processExited( TDEProcess* proc )
  374. {
  375. delete proc;
  376. }
  377. #include "boxcontaineritem.moc"