[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    New feature for KDE 3.5.4 - KAlarm
From:       David Jarvie <lists () astrojar ! org ! uk>
Date:       2006-06-16 23:29:59
Message-ID: 200606170030.00313.lists () astrojar ! org ! uk
[Download RAW message or body]

I'd like to add a new feature to KAlarm for KDE 3.5.4 (patch attached). It is 
the facility to import alarms from external calendars. It was introduced in 
the standalone version of KAlarm over 2 months ago, and has been ported to 
trunk. It has a small number of new translatable strings.

Cornelius, as the maintainer for kdepim, can you please give the go-ahead to 
add this feature?

-- 
David Jarvie.
KAlarm author and maintainer.
http://www.astrojar.org.uk/linux/kalarm.html

["kalarm-import.diff" (text/x-diff)]

diff -u /home/david/src/svn/3.5/kdepim/kalarm/alarmcalendar.cpp ./alarmcalendar.cpp
--- /home/david/src/svn/3.5/kdepim/kalarm/alarmcalendar.cpp	2006-06-08 \
                22:07:37.000000000 +0100
+++ ./alarmcalendar.cpp	2006-06-16 18:53:41.000000000 +0100
@@ -1,7 +1,7 @@
 /*
  *  alarmcalendar.cpp  -  KAlarm calendar file access
  *  Program:  kalarm
- *  Copyright (c) 2001-2006 by David Jarvie <software@astrojar.org.uk>
+ *  Copyright © 2001-2006 by David Jarvie <software@astrojar.org.uk>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -36,6 +36,7 @@
 #include <kio/netaccess.h>
 #include <kfileitem.h>
 #include <ktempfile.h>
+#include <kfiledialog.h>
 #include <dcopclient.h>
 #include <kdebug.h>
 
@@ -49,6 +50,7 @@
 
 #include "calendarcompat.h"
 #include "daemon.h"
+#include "functions.h"
 #include "kalarmapp.h"
 #include "mainwindow.h"
 #include "preferences.h"
@@ -430,6 +432,135 @@
 }
 
 /******************************************************************************
+* Import alarms from an external calendar and merge them into KAlarm's calendar.
+* The alarms are given new unique event IDs.
+* Parameters: parent = parent widget for error message boxes
+* Reply = true if all alarms in the calendar were successfully imported
+*       = false if any alarms failed to be imported.
+*/
+bool AlarmCalendar::importAlarms(QWidget* parent)
+{
+	KURL url = KFileDialog::getOpenURL(QString::fromLatin1(":importalarms"),
+	                                   QString::fromLatin1("*.vcs \
*.ics|%1").arg(i18n("Calendar Files")), parent); +	if (url.isEmpty())
+	{
+		kdError(5950) << "AlarmCalendar::importAlarms(): Empty URL" << endl;
+		return false;
+	}
+	if (!url.isValid())
+	{
+		kdDebug(5950) << "AlarmCalendar::importAlarms(): Invalid URL" << endl;
+		return false;
+	}
+	kdDebug(5950) << "AlarmCalendar::importAlarms(" << url.prettyURL() << ")" << endl;
+
+	bool success = true;
+	QString filename;
+	bool local = url.isLocalFile();
+	if (local)
+	{
+		filename = url.path();
+		if (!KStandardDirs::exists(filename))
+		{
+			kdDebug(5950) << "AlarmCalendar::importAlarms(): File '" << url.prettyURL() << "' \
not found" << endl; +			KMessageBox::error(parent, i18n("Could not load calendar \
'%1'.").arg(url.prettyURL())); +			return false;
+		}
+	}
+	else
+	{
+		if (!KIO::NetAccess::download(url, filename, MainWindow::mainMainWindow()))
+		{
+			kdError(5950) << "AlarmCalendar::importAlarms(): Download failure" << endl;
+			KMessageBox::error(parent, i18n("Cannot download \
calendar:\n%1").arg(url.prettyURL())); +			return false;
+		}
+		kdDebug(5950) << "--- Downloaded to " << filename << endl;
+	}
+
+	// Read the calendar and add its alarms to the current calendars
+	CalendarLocal cal(QString::fromLatin1("UTC"));
+	cal.setLocalTime();    // write out using local time (i.e. no time zone)
+	success = cal.load(filename);
+	if (!success)
+	{
+		kdDebug(5950) << "AlarmCalendar::importAlarms(): error loading calendar '" << \
filename << "'" << endl; +		KMessageBox::error(parent, i18n("Could not load calendar \
'%1'.").arg(url.prettyURL())); +	}
+	else
+	{
+		CalendarCompat::fix(cal, filename);
+		bool saveActive   = false;
+		bool saveExpired  = false;
+		bool saveTemplate = false;
+		AlarmCalendar* active  = activeCalendar();
+		AlarmCalendar* expired = expiredCalendar();
+		AlarmCalendar* templat = 0;
+		AlarmCalendar* acal;
+		Event::List events = cal.rawEvents();
+		for (Event::List::ConstIterator it = events.begin();  it != events.end();  ++it)
+		{
+			const Event* event = *it;
+			if (event->alarms().isEmpty())
+				continue;    // ignore events without alarms
+			KAEvent::Status type = KAEvent::uidStatus(event->uid());
+			switch (type)
+			{
+				case KAEvent::ACTIVE:
+					acal = active;
+					saveActive = true;
+					break;
+				case KAEvent::EXPIRED:
+					acal = expired;
+					saveExpired = true;
+					break;
+				case KAEvent::TEMPLATE:
+					if (!templat)
+						templat = templateCalendarOpen();
+					acal = templat;
+					saveTemplate = true;
+					break;
+				default:
+					continue;
+			}
+			if (!acal)
+				continue;
+
+			Event* newev = new Event(*event);
+
+			// If there is a display alarm without display text, use the event
+			// summary text instead.
+			if (type == KAEvent::ACTIVE  &&  !newev->summary().isEmpty())
+			{
+				const Alarm::List& alarms = newev->alarms();
+				for (Alarm::List::ConstIterator ait = alarms.begin();  ait != alarms.end();  \
++ait) +				{
+					Alarm* alarm = *ait;
+					if (alarm->type() == Alarm::Display  &&  alarm->text().isEmpty())
+						alarm->setText(newev->summary());
+				}
+				newev->setSummary(QString::null);   // KAlarm only uses summary for template \
names +			}
+			// Give the event a new ID and add it to the calendar
+			newev->setUid(KAEvent::uid(CalFormat::createUniqueId(), type));
+			if (!acal->mCalendar->addEvent(newev))
+				success = false;
+		}
+
+		// Save any calendars which have been modified
+		if (saveActive)
+			active->saveCal();
+		if (saveExpired)
+			expired->saveCal();
+		if (saveTemplate)
+			templat->saveCal();
+	}
+	if (!local)
+		KIO::NetAccess::removeTempFile(filename);
+	return success;
+}
+
+/******************************************************************************
 * Flag the start of a group of calendar update calls.
 * The purpose is to avoid multiple calendar saves during a group of operations.
 */
diff -u /home/david/src/svn/3.5/kdepim/kalarm/alarmcalendar.h ./alarmcalendar.h
--- /home/david/src/svn/3.5/kdepim/kalarm/alarmcalendar.h	2006-06-08 \
                22:07:36.000000000 +0100
+++ ./alarmcalendar.h	2006-06-16 18:55:34.000000000 +0100
@@ -1,7 +1,7 @@
 /*
  *  alarmcalendar.h  -  KAlarm calendar file access
  *  Program:  kalarm
- *  Copyright (c) 2001 - 2005 by David Jarvie <software@astrojar.org.uk>
+ *  Copyright © 2001-2006 by David Jarvie <software@astrojar.org.uk>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -71,6 +71,7 @@
 		static AlarmCalendar* expiredCalendarOpen()   { return calendarOpen(EXPIRED); }
 		static AlarmCalendar* displayCalendarOpen()   { return calendarOpen(DISPLAY); }
 		static AlarmCalendar* templateCalendarOpen()  { return calendarOpen(TEMPLATE); }
+		static bool           importAlarms(QWidget*);
 		static const KCal::Event* getEvent(const QString& uniqueID);
 
 		enum CalID { ACTIVE, EXPIRED, DISPLAY, TEMPLATE, NCALS };
diff -u /home/david/src/svn/3.5/kdepim/kalarm/kalarmui.rc ./kalarmui.rc
--- /home/david/src/svn/3.5/kdepim/kalarm/kalarmui.rc	2006-06-08 22:07:36.000000000 \
                +0100
+++ ./kalarmui.rc	2006-03-14 20:12:25.000000000 +0000
@@ -17,6 +17,7 @@
   <Menu name="file" >
    <text>&amp;File</text>
    <Action name="templates" />
+   <Action name="importAlarms" />
    <Action name="importBirthdays" />
   </Menu>
   <Menu name="view" >
diff -u /home/david/src/svn/3.5/kdepim/kalarm/mainwindow.cpp ./mainwindow.cpp
--- /home/david/src/svn/3.5/kdepim/kalarm/mainwindow.cpp	2006-06-08 \
                22:07:36.000000000 +0100
+++ ./mainwindow.cpp	2006-06-16 19:04:49.000000000 +0100
@@ -318,6 +318,7 @@
 	mActionShowExpired->setCheckedState(i18n_e_HideExpiredAlarms());
 	mActionToggleTrayIcon  = new KToggleAction(i18n("Show in System &Tray"), \
Qt::CTRL+Qt::Key_Y, this, SLOT(slotToggleTrayIcon()), actions, "showInSystemTray");  \
mActionToggleTrayIcon->setCheckedState(i18n("Hide From System &Tray")); +	new \
KAction(i18n("Import &Alarms..."), 0, this, SLOT(slotImportAlarms()), actions, \
"importAlarms");  new KAction(i18n("Import &Birthdays..."), 0, this, \
SLOT(slotBirthdays()), actions, "importBirthdays");  new KAction(i18n("&Refresh \
Alarms"), "reload", 0, this, SLOT(slotResetDaemon()), actions, "refreshAlarms");  \
Daemon::createAlarmEnableAction(actions, "alarmEnable"); @@ -788,6 +789,16 @@
 }
 
 /******************************************************************************
+*  Called when the Import Alarms menu item is selected, to merge alarms from an
+*  external calendar into the current calendars.
+*/
+void MainWindow::slotImportAlarms()
+{
+	if (AlarmCalendar::importAlarms(this))
+		mListView->refresh();
+}
+
+/******************************************************************************
 *  Called when the Import Birthdays menu item is selected, to display birthdays
 *  from the address book for selection as alarms.
 */
diff -u /home/david/src/svn/3.5/kdepim/kalarm/mainwindow.h ./mainwindow.h
--- /home/david/src/svn/3.5/kdepim/kalarm/mainwindow.h	2006-06-08 22:07:36.000000000 \
                +0100
+++ ./mainwindow.h	2006-06-16 18:57:09.000000000 +0100
@@ -104,6 +104,7 @@
 		void           slotEnable();
 		void           slotToggleTrayIcon();
 		void           slotResetDaemon();
+		void           slotImportAlarms();
 		void           slotBirthdays();
 		void           slotTemplates();
 		void           slotTemplatesEnd();



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic