Fix initial state of actions (and window title).
[kdepim.git] / knotes / resourcelocal.cpp
blob22cd9ed66da567b769e3c4cb95bc79f855fe2af6
1 /*******************************************************************
2 This file is part of KNotes.
4 Copyright (c) 2004, Bo Thorsen <bo@sonofthor.dk>
5 2004-2006, Michael Brade <brade@kde.org>
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.
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.
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.
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 Qt library by Trolltech AS, Norway (or with modified versions
25 of Qt that use the same license as Qt), 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 Qt. 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 *******************************************************************/
34 #include "knotes/resourcelocal.h"
35 #include "knotes/resourcelocalconfig.h"
36 #include "knotes/resourcemanager.h"
37 #include "knotes/resourcenotes.h"
39 #include <klocale.h>
40 #include <kmessagebox.h>
41 #include <kstandarddirs.h>
42 #include <kconfiggroup.h>
44 #include <kcal/icalformat.h>
47 ResourceLocal::ResourceLocal()
48 : ResourceNotes(), mCalendar( QString::fromLatin1( "UTC" ) )
50 kDebug( 5500 ) << "ResourceLocal::ResourceLocal()";
51 setType( "file" );
52 mURL = KUrl::fromPath( KGlobal::dirs()->saveLocation( "data", "knotes/" ) +
53 "notes.ics" );
56 ResourceLocal::ResourceLocal( const KConfigGroup &group )
57 : ResourceNotes( group ), mCalendar( QString::fromLatin1( "UTC" ) )
59 kDebug( 5500 ) << "ResourceLocal::ResourceLocal()";
60 setType( "file" );
61 mURL = KUrl::fromPath( KGlobal::dirs()->saveLocation( "data", "knotes/" ) +
62 "notes.ics" );
64 KUrl u = group.readPathEntry( "NotesURL", QString() );
65 if ( !u.isEmpty() ) {
66 mURL = u;
70 ResourceLocal::~ResourceLocal()
74 void ResourceLocal::writeConfig( KConfigGroup &group )
76 KRES::Resource::writeConfig( group );
77 group.writePathEntry( "NotesURL", mURL.prettyUrl() );
80 bool ResourceLocal::load()
82 mCalendar.load( mURL.toLocalFile() );
84 KCal::Journal::List notes = mCalendar.journals();
85 KCal::Journal::List::ConstIterator it;
86 for ( it = notes.constBegin(); it != notes.constEnd(); ++it ) {
87 manager()->registerNote( this, *it );
90 return true;
93 bool ResourceLocal::save()
95 if ( !mCalendar.save( mURL.toLocalFile(), new KCal::ICalFormat() ) ) {
96 KMessageBox::error( 0, i18n( "<qt>Unable to save the notes to <b>%1</b>. "
97 "Check that there is sufficient disk space."
98 "<br />There should be a backup in the same "
99 "directory though.</qt>", mURL.toLocalFile() ) );
100 return false;
103 return true;
106 bool ResourceLocal::addNote( KCal::Journal *journal )
108 return mCalendar.addJournal( journal );
111 bool ResourceLocal::deleteNote( KCal::Journal *journal )
113 return mCalendar.deleteJournal( journal );
116 KCal::Alarm::List ResourceLocal::alarms( const KDateTime &from,
117 const KDateTime &to )
119 KCal::Alarm::List alarms;
120 KCal::Journal::List notes = mCalendar.journals();
121 KCal::Journal::List::ConstIterator note;
123 for ( note = notes.constBegin(); note != notes.constEnd(); ++note ) {
124 KDateTime preTime = from.addSecs( -1 );
125 KCal::Alarm::List::ConstIterator it;
126 for( it = ( *note )->alarms().constBegin();
127 it != ( *note )->alarms().constEnd(); ++it ) {
128 if ( ( *it )->enabled() ) {
129 KDateTime dt = ( *it )->nextRepetition( preTime );
130 if ( dt.isValid() && dt <= to ) {
131 alarms.append( *it );
137 return alarms;