knotes/summarywidget.cpp
1 /*
2  This file is part of Kontact.
3  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tqobject.h>
25 #include <tqlabel.h>
26 #include <tqlayout.h>
27 #include <tqtooltip.h>
28 
29 #include <dcopclient.h>
30 #include <dcopref.h>
31 #include <tdeapplication.h>
32 #include <kdebug.h>
33 #include <tdeglobal.h>
34 #include <kiconloader.h>
35 #include <tdelocale.h>
36 #include <kurllabel.h>
37 #include <kstandarddirs.h>
38 
39 #include <knotes/resourcenotes.h>
40 #include <knotes/resourcemanager.h>
41 
42 #include "core.h"
43 #include "plugin.h"
44 
45 #include "summarywidget.h"
46 
47 KNotesSummaryWidget::KNotesSummaryWidget( Kontact::Plugin *plugin,
48  TQWidget *parent, const char *name )
49  : Kontact::Summary( parent, name ), mLayout( 0 ), mPlugin( plugin )
50 {
51  TQVBoxLayout *mainLayout = new TQVBoxLayout( this, 3, 3 );
52 
53  TQPixmap icon = TDEGlobal::iconLoader()->loadIcon( "kontact_notes",
54  TDEIcon::Desktop, TDEIcon::SizeMedium );
55  TQWidget* header = createHeader( this, icon, i18n( "Notes" ) );
56  mainLayout->addWidget( header );
57 
58  mLayout = new TQGridLayout( mainLayout, 7, 3, 3 );
59  mLayout->setRowStretch( 6, 1 );
60 
61  mCalendar = new KCal::CalendarLocal( TQString::fromLatin1("UTC") );
62  KNotesResourceManager *manager = new KNotesResourceManager();
63 
64  TQObject::connect( manager, TQ_SIGNAL( sigRegisteredNote( KCal::Journal* ) ),
65  this, TQ_SLOT( addNote( KCal::Journal* ) ) );
66  TQObject::connect( manager, TQ_SIGNAL( sigDeregisteredNote( KCal::Journal* ) ),
67  this, TQ_SLOT( removeNote( KCal::Journal* ) ) );
68  manager->load();
69 
70 
71  updateView();
72 }
73 
74 void KNotesSummaryWidget::updateView()
75 {
76  mNotes = mCalendar->journals();
77  TQLabel *label;
78 
79  for ( label = mLabels.first(); label; label = mLabels.next() )
80  label->deleteLater();
81  mLabels.clear();
82 
83  TDEIconLoader loader( "knotes" );
84 
85  int counter = 0;
86  TQPixmap pm = loader.loadIcon( "knotes", TDEIcon::Small );
87 
88  KCal::Journal::List::Iterator it;
89  if ( mNotes.count() ) {
90  for (it = mNotes.begin(); it != mNotes.end(); ++it) {
91 
92  // Fill Note Pixmap Field
93  label = new TQLabel( this );
94  label->setPixmap( pm );
95  label->setMaximumWidth( label->minimumSizeHint().width() );
96  label->setAlignment( AlignVCenter );
97  mLayout->addWidget( label, counter, 0 );
98  mLabels.append( label );
99 
100  // File Note Summary Field
101  TQString newtext = (*it)->summary();
102 
103  KURLLabel *urlLabel = new KURLLabel( (*it)->uid(), newtext, this );
104  urlLabel->installEventFilter( this );
105  urlLabel->setTextFormat(RichText);
106  urlLabel->setAlignment( urlLabel->alignment() | TQt::WordBreak );
107  mLayout->addWidget( urlLabel, counter, 1 );
108  mLabels.append( urlLabel );
109 
110  if ( !(*it)->description().isEmpty() ) {
111  TQToolTip::add( urlLabel, (*it)->description().left( 80 ) );
112  }
113 
114  connect( urlLabel, TQ_SIGNAL( leftClickedURL( const TQString& ) ),
115  this, TQ_SLOT( urlClicked( const TQString& ) ) );
116  counter++;
117  }
118 
119  } else {
120  TQLabel *noNotes = new TQLabel( i18n( "No Notes Available" ), this );
121  noNotes->setAlignment( AlignHCenter | AlignVCenter );
122  mLayout->addWidget( noNotes, 0, 1 );
123  mLabels.append( noNotes );
124  }
125 
126  for ( label = mLabels.first(); label; label = mLabels.next() )
127  label->show();
128 }
129 
130 void KNotesSummaryWidget::urlClicked( const TQString &/*uid*/ )
131 {
132  if ( !mPlugin->isRunningStandalone() )
133  mPlugin->core()->selectPlugin( mPlugin );
134  else
135  mPlugin->bringToForeground();
136 }
137 
138 bool KNotesSummaryWidget::eventFilter( TQObject *obj, TQEvent* e )
139 {
140  if ( obj->inherits( "KURLLabel" ) ) {
141  KURLLabel* label = static_cast<KURLLabel*>( obj );
142  if ( e->type() == TQEvent::Enter )
143  emit message( i18n( "Read Note: \"%1\"" ).arg( label->text() ) );
144  if ( e->type() == TQEvent::Leave )
145  emit message( TQString() );
146  }
147 
148  return Kontact::Summary::eventFilter( obj, e );
149 }
150 
151 void KNotesSummaryWidget::addNote( KCal::Journal *j )
152 {
153  mCalendar->addJournal( j );
154  updateView();
155 }
156 
157 void KNotesSummaryWidget::removeNote( KCal::Journal *j )
158 {
159  mCalendar->deleteJournal( j );
160  updateView();
161 }
162 
163 
164 #include "summarywidget.moc"
Base class for all Plugins in Kontact.
Definition: plugin.h:59