summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/calendar/calformatter.cpp
blob: fa02c2cff4e1bbfd1799ef1259cef6db38012174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* ============================================================
 * File  : calformatter.cpp
 * Author: Maciek Borowka <maciek_AT_borowka.net>
 * Date  : 2005-11-23
 * Description : The implementation of a class that decides which
 * cell of the calendar should be painted with which color.
 *
 * Copyright 2005 by Maciek Borowka
 * Revised by Orgad Shaneh, 2007
 *
 * 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 the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

#include <qdatetime.h>
#include <qcolor.h>
#include <qmap.h>

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kcalendarsystem.h>
#include <libkcal/calendarlocal.h>

#include "calformatter.h"


namespace KIPICalendarPlugin {


class CalFormatter::Data
{
public:
    class Day {
    public:
        Day(){};
        Day(QColor c, QString d): color(c), description(d) {};

        QColor color;
        QString description;
     };


    QString ohFile;
    QString fhFile;

    QMap<QDate, Day> oh;//official holidays
    QMap<QDate, Day> fh;//family holidays

    Data()
    {

//you define a holiday that way:
oh[QDate(2005, 1, 1)] = Day(Qt::red, "New year!");
//oh[QDate(2005, 1, 4)] = Day(Qt::red, "Fete Nat");


fh[QDate(2005,1,3)] = Day(Qt::green, "Adam");
//fh[QDate(2005, 1, 14)] = Day(Qt::green, "Maciek");


    };

};

CalFormatter::CalFormatter() : initialized(false)
{

}


CalFormatter::~CalFormatter()
{
}

void CalFormatter::init(int year, const QString & ohFile, const QString & fhFile)
{
    if (initialized)
        return;

    d = new Data();
    year_ = year;
    d->ohFile = ohFile;
    d->fhFile = fhFile;

    kdDebug(51000) << "Loading calendar from file" << endl;
    KCal::CalendarLocal * calendar;

    if (not(ohFile.isEmpty())) {
        calendar = new KCal::CalendarLocal("UTC");
        if (calendar->load(ohFile)) {
                QDate dtFirst, dtLast;
                KGlobal::locale()->calendar()->setYMD(dtFirst, year_, 1, 1);
                KGlobal::locale()->calendar()->setYMD(dtLast, year_ + 1, 1, 1);
                dtLast = dtLast.addDays(-1);

                KCal::Event::List list = calendar->rawEvents(dtFirst, dtLast);
                KCal::Event::List::iterator it;
		KCal::Recurrence *recur;

		QDateTime dtCurrent;
                int counter = 0;
                for ( it = list.begin(); it != list.end(); ++it )
                {
                        kdDebug(51000) << (*it)->summary() << endl << "--------" << endl;
                        counter++;
			if ((*it)->doesRecur())
			{
				recur = (*it)->recurrence();
				for (dtCurrent = recur->getNextDateTime(dtFirst.addDays(-1)); (dtCurrent <= dtLast) && dtCurrent.isValid(); dtCurrent = recur->getNextDateTime(dtCurrent))
				{
					kdDebug(51000) << dtCurrent.toString() << endl;
					d->oh[dtCurrent.date()] = CalFormatter::Data::Day(Qt::red, (*it)->summary());
				}
				
			}
			else
	                        d->oh[(*it)->dtStart().date()] = CalFormatter::Data::Day(Qt::red, (*it)->summary());
                }
                kdDebug(51000) << "Loaded " << counter << " events for year " << year_ << endl;
        }
        delete calendar;
    }

    if (not(fhFile.isEmpty())) {
        calendar = new KCal::CalendarLocal("UTC");
        if (calendar->load(fhFile)) {

                KCal::Event::List list = calendar->rawEvents(QDate(year_,1,1), QDate(year_,12,31));
                KCal::Event::List::iterator it;

                QString eventDate;
                int counter = 0;
                for ( it = list.begin(); it != list.end(); ++it )
                {
                        counter++;
                        d->fh[(*it)->dtStart().date()] = CalFormatter::Data::Day(Qt::red, (*it)->summary());
                        //kdDebug(51000) << eventDate << "----" <<  (*it)->summary() << endl;
                }
                kdDebug(51000) << "Loaded " << counter << " events for year " << year_ << endl;
        }
        delete calendar;
    }
}

bool CalFormatter::isPrayDay(int month, int day)
{
    QDate dt;
    KGlobal::locale()->calendar()->setYMD(dt, year_, month, day);
    return (dt.dayOfWeek() == KGlobal::locale()->calendar()->weekDayOfPray());
}

/*!
    Returns true if special formatting is to be applied to the particular day
 */
bool KIPICalendarPlugin::CalFormatter::isSpecial(int month, int day)
{
    QDate dt;
    KGlobal::locale()->calendar()->setYMD(dt, year_, month, day);

    return (isPrayDay(month,day) || d->oh.contains(dt) || d->fh.contains(dt));
}


/*!
    Returns the color to be used for painting of the day info
 */
QColor KIPICalendarPlugin::CalFormatter::getDayColor(int month, int day)
{
    QDate dt;
    KGlobal::locale()->calendar()->setYMD(dt, year_, month, day);

    if (isPrayDay(month, day))
        return Qt::red;

    if (d->oh.contains(dt))
        return Qt::red;

    if (d->fh.contains(dt))
        return Qt::green;

    //default
    return Qt::black;
}


/*!
    Returns the description of the day to be painted on the calendar.
 */
QString KIPICalendarPlugin::CalFormatter::getDayDescr(int month, int day)
{
    QDate dt;
    KGlobal::locale()->calendar()->setYMD(dt, year_, month, day);

    QString ret;

    if (d->oh.contains(dt))
        ret = d->oh[dt].description;

    if (d->fh.contains(dt)) {
        if (ret.isNull()) 
            return d->fh[dt].description;
        else
            return ret.append(";").append(d->fh[dt].description);
    }

    return ret;
}
}