knotes

resourcemanager.cpp
1 /*******************************************************************
2  This file is part of KNotes.
3 
4  Copyright (c) 2004, Bo Thorsen <bo@sonofthor.dk>
5  2004, Michael Brade <brade@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  MA 02110-1301, USA.
21 
22  In addition, as a special exception, the copyright holders give
23  permission to link the code of this program with any edition of
24  the TQt library by Trolltech AS, Norway (or with modified versions
25  of TQt that use the same license as TQt), and distribute linked
26  combinations including the two. You must obey the GNU General
27  Public License in all respects for all of the code used other than
28  TQt. If you modify this file, you may extend this exception to
29  your version of the file, but you are not obligated to do so. If
30  you do not wish to do so, delete this exception statement from
31  your version.
32 *******************************************************************/
33 
34 #include "knotes/resourcemanager.h"
35 #include "knotes/resourcelocal.h"
36 
37 #include <libkcal/journal.h>
38 
39 
40 KNotesResourceManager::KNotesResourceManager()
41  : TQObject( 0, "KNotes Resource Manager" )
42 {
43  m_manager = new KRES::Manager<ResourceNotes>( "notes" );
44  m_manager->addObserver( this );
45  m_manager->readConfig();
46 }
47 
48 KNotesResourceManager::~KNotesResourceManager()
49 {
50  delete m_manager;
51 }
52 
53 void KNotesResourceManager::load()
54 {
55  if ( !m_manager->standardResource() )
56  {
57  kdWarning(5500) << "No standard resource yet." << endl;
58  ResourceNotes *resource = new ResourceLocal( 0 );
59  m_manager->add( resource );
60  m_manager->setStandardResource( resource );
61  }
62 
63  // Open all active resources
64  KRES::Manager<ResourceNotes>::ActiveIterator it;
65  for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
66  {
67  if ( (*it)->isOpen() ) {
68  kdDebug(5500) << (*it)->resourceName() << " is already open" << endl;
69  continue;
70  }
71 
72  kdDebug(5500) << "Opening resource " + (*it)->resourceName() << endl;
73  (*it)->setManager( this );
74  if ( (*it)->open() )
75  (*it)->load();
76  }
77 }
78 
79 void KNotesResourceManager::save()
80 {
81  KRES::Manager<ResourceNotes>::ActiveIterator it;
82  for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
83  (*it)->save();
84 }
85 
86 // when adding a new note, make sure a config file exists!!
87 
88 bool KNotesResourceManager::addNewNote( KCal::Journal *journal )
89 {
90  // TODO: Make this configurable
91  ResourceNotes *resource = m_manager->standardResource();
92  if ( resource ) {
93  if ( resource->addNote( journal ) ) {
94  registerNote( resource, journal );
95  return true;
96  }
97  } else {
98  kdWarning(5500) << k_funcinfo << "no resource!" << endl;
99  }
100  return false;
101 }
102 
103 void KNotesResourceManager::registerNote( ResourceNotes *resource,
104  KCal::Journal *journal )
105 {
106  // TODO: only emit the signal if the journal is new?
107  m_resourceMap.insert( journal->uid(), resource );
108  emit sigRegisteredNote( journal );
109 }
110 
111 void KNotesResourceManager::deleteNote( KCal::Journal *journal )
112 {
113  if ( !journal )
114  return;
115 
116  TQString uid = journal->uid();
117 
118  // Remove the journal from the resource it came from
119  ResourceNotes *res = m_resourceMap[ uid ];
120  if ( res ) {
121  res->deleteNote( journal );
122  m_resourceMap.remove( uid );
123 
124  // libkcal does not delete the journal immediately, therefore it is ok to
125  // emit the journal here
126  emit sigDeregisteredNote( journal );
127  }
128 }
129 
130 KCal::Alarm::List KNotesResourceManager::alarms( const TQDateTime& from, const TQDateTime& to )
131 {
132  KCal::Alarm::List result;
133 
134  KRES::Manager<ResourceNotes>::ActiveIterator it;
135  for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
136  {
137  KCal::Alarm::List list = (*it)->alarms( from, to );
138  KCal::Alarm::List::ConstIterator it;
139  for ( it = list.constBegin(); it != list.constEnd(); ++it )
140  result.append( *it );
141  }
142 
143  return result;
144 }
145 
146 void KNotesResourceManager::resourceAdded( ResourceNotes *resource )
147 {
148  kdDebug(5500) << "Resource added: " << resource->resourceName() << endl;
149 
150  if ( !resource->isActive() )
151  return;
152 
153  if ( resource->isOpen() ) {
154  kdDebug(5500) << resource->resourceName() << " is already open" << endl;
155  return;
156  }
157 
158  resource->setManager( this );
159  if ( resource->open() )
160  resource->load();
161 }
162 
163 void KNotesResourceManager::resourceModified( ResourceNotes *resource )
164 {
165  kdDebug(5500) << "Resource modified: " << resource->resourceName() << endl;
166 }
167 
168 void KNotesResourceManager::resourceDeleted( ResourceNotes *resource )
169 {
170  kdDebug(5500) << "Resource deleted: " << resource->resourceName() << endl;
171 }
172 
173 
174 #include "resourcemanager.moc"
This class provides the interfaces for a KNotes resource.
Definition: resourcenotes.h:56