knotes

resourcelocal.cpp
1/*******************************************************************
2 This file is part of KNotes.
3
4 Copyright (c) 2004, Bo Thorsen <bo@sonofthor.dk>
5 2004-2006, 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 <tdelocale.h>
35#include <tdemessagebox.h>
36#include <tdestandarddirs.h>
37
38#include <libkcal/icalformat.h>
39
40#include "knotes/resourcelocal.h"
41#include "knotes/resourcelocalconfig.h"
42#include "knotes/resourcemanager.h"
43#include "knotes/resourcenotes.h"
44
45
46
47ResourceLocal::ResourceLocal( const TDEConfig *config )
48 : ResourceNotes( config ), mCalendar( TQString::fromLatin1( "UTC" ) )
49{
50 kdDebug(5500) << "ResourceLocal::ResourceLocal()" << endl;
51 setType( "file" );
52 mURL = TDEGlobal::dirs()->saveLocation( "data", "knotes/" ) + "notes.ics";
53
54 if ( config )
55 {
56 KURL u = config->readPathEntry( "NotesURL" );
57 if ( !u.isEmpty() )
58 mURL = u;
59 }
60}
61
62ResourceLocal::~ResourceLocal()
63{
64}
65
66void ResourceLocal::writeConfig( TDEConfig *config )
67{
68 KRES::Resource::writeConfig( config );
69 config->writePathEntry( "NotesURL", mURL.prettyURL() );
70}
71
72bool ResourceLocal::load()
73{
74 mCalendar.load( mURL.path() );
75
76 KCal::Journal::List notes = mCalendar.journals();
77 KCal::Journal::List::ConstIterator it;
78 for ( it = notes.constBegin(); it != notes.constEnd(); ++it )
79 manager()->registerNote( this, *it );
80
81 return true;
82}
83
84bool ResourceLocal::save()
85{
86 if ( !mCalendar.save( mURL.path(), new KCal::ICalFormat() ) )
87 {
88 KMessageBox::error( 0,
89 i18n("<qt>Unable to save the notes to <b>%1</b>. "
90 "Check that there is sufficient disk space."
91 "<br>There should be a backup in the same directory "
92 "though.</qt>").arg( mURL.path() ) );
93 return false;
94 }
95
96 return true;
97}
98
99bool ResourceLocal::addNote( KCal::Journal *journal )
100{
101 return mCalendar.addJournal( journal );
102}
103
104bool ResourceLocal::deleteNote( KCal::Journal *journal )
105{
106 return mCalendar.deleteJournal( journal );
107}
108
109KCal::Alarm::List ResourceLocal::alarms( const TQDateTime& from, const TQDateTime& to )
110{
111 KCal::Alarm::List alarms;
112 KCal::Journal::List notes = mCalendar.journals();
113 KCal::Journal::List::ConstIterator note;
114 for ( note = notes.constBegin(); note != notes.constEnd(); ++note )
115 {
116 TQDateTime preTime = from.addSecs( -1 );
117 KCal::Alarm::List::ConstIterator it;
118 for( it = (*note)->alarms().constBegin(); it != (*note)->alarms().constEnd(); ++it )
119 {
120 if ( (*it)->enabled() )
121 {
122 TQDateTime dt = (*it)->nextRepetition( preTime );
123 if ( dt.isValid() && dt <= to )
124 alarms.append( *it );
125 }
126 }
127 }
128
129 return alarms;
130}
This class provides the interfaces for a KNotes resource.
Definition: resourcenotes.h:56