summaryrefslogtreecommitdiff
path: root/core/pim/today/plugins/addressbook/addresspluginwidget.cpp
blob: 5662af28dbd1d17f8581826c78b651b0c21e2f04 (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
/*
 * addresspluginwidget.cpp
 *
 * copyright   : (c) 2003 by Stefan Eilers
 * email       : eilers.stefan@epost.de
 *
 * This implementation was derived from the todolist plugin implementation
 *
 */
/***************************************************************************
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "addresspluginwidget.h"

#include <opie2/odebug.h>
#include <opie2/opimcontact.h>

#include <qpe/config.h>

AddressBookPluginWidget::AddressBookPluginWidget( QWidget *parent,  const char* name )
    : QWidget( parent, name ) {

    addressLabel = 0l;
    m_contactdb = 0l;
    layoutTodo = 0l;

    m_contactdb = new Opie::OPimContactAccess("addressplugin");

    connect( m_contactdb, SIGNAL( signalChanged(const Opie::OPimContactAccess*) ),
	     this, SLOT( refresh(const Opie::OPimContactAccess*) ) );


    readConfig();
    getAddress();
}

AddressBookPluginWidget::~AddressBookPluginWidget() {
    delete m_contactdb;
}

void AddressBookPluginWidget::refresh( const Opie::OPimContactAccess* )
{
	owarn << " AddressBookPluginWidget::Database was changed externally ! " << oendl;
	m_contactdb->reload();
	getAddress();
}

void AddressBookPluginWidget::reinitialize() {
	readConfig();
	getAddress();
}

void AddressBookPluginWidget::readConfig() {
    Config cfg( "todayaddressplugin" );
    cfg.setGroup( "config" );
    m_maxLinesTask  = cfg.readNumEntry( "maxlinestask", 5 );
    m_maxCharClip   = cfg.readNumEntry( "maxcharclip", 38 );
    m_daysLookAhead = cfg.readNumEntry( "dayslookahead", 14 );
    m_urgentDays    = cfg.readNumEntry( "urgentdays", 7 );
    m_entryColor    = cfg.readEntry("entrycolor", Qt::black.name() );
    m_headlineColor = cfg.readEntry( "headlinecolor", Qt::black.name() );
    m_urgentColor   = cfg.readEntry( "urgentcolor", Qt::red.name() );
    m_showBirthdays = cfg.readBoolEntry( "showBirthdays", true );
    m_showAnniversaries = cfg.readBoolEntry( "showAnniversaries", true );
}


/**
 * Get the addresss
 */
void AddressBookPluginWidget::getAddress() {

	if ( ! layoutTodo ){
		layoutTodo = new QVBoxLayout( this );
	}

	if ( ! addressLabel ) {
		addressLabel = new Opie::Ui::OClickableLabel( this );
		connect( addressLabel, SIGNAL( clicked() ), this, SLOT( startAddressBook() ) );
		layoutTodo->addWidget( addressLabel );
	}

	QString output;

	// Check whether the database provide the search option..
    if ( !m_contactdb->hasQuerySettings( Opie::OPimContactAccess::DateDiff ) ){
		// Libopie seems to be old..
		output = QObject::tr( "Database does not provide this search query ! Please upgrade libOpie !<br>" );
		addressLabel->setText( output );
		return;
	}

	// Define the query for birthdays and start search..
	QDate lookAheadDate = QDate::currentDate().addDays( m_daysLookAhead );
	int ammount = 0;
	if ( m_showBirthdays ){
		owarn << "Searching from now (" << QDate::currentDate().toString() << ") until "
						<< lookAheadDate.toString() << " ! " << oendl;

		if ( m_contactdb->hasQuerySettings( Opie::OPimContactAccess::DateDiff ) ){


			Opie::OPimContact querybirthdays;
			querybirthdays.setBirthday( lookAheadDate );

			m_list = m_contactdb->queryByExample( querybirthdays,
								  Opie::OPimContactAccess::DateDiff );
			if ( m_list.count() > 0 ){
				output = "<font color=" + m_headlineColor + ">"
					+ QObject::tr( "Next birthdays in <b> %1 </b> days:" )
					.arg( m_daysLookAhead )
					+ "</font> <br>";
				for ( m_it = m_list.begin(); m_it != m_list.end(); ++m_it ) {
					if ( ammount++ < m_maxLinesTask ){
						// Now we want to calculate how many days
						//until birthday. We have to set
						// the correct year to calculate the day diff...
						QDate destdate = (*m_it).birthday();
						destdate.setYMD( QDate::currentDate().year(),
								 destdate.month(), destdate.day() );
						if ( QDate::currentDate().daysTo(destdate) < 0 )
							destdate.setYMD( QDate::currentDate().year()+1,
									 destdate.month(), destdate.day() );


						if ( QDate::currentDate().daysTo(destdate) < m_urgentDays )
							output += "<font color=" + m_urgentColor + "><b>-"
								+ (*m_it).fullName()
								+ " ("
								+ QString::number(QDate::currentDate()
										  .daysTo(destdate))
								+ " Days) </b></font><br>";

						else
							output += "<font color=" + m_entryColor + "><b>-"
								+ (*m_it).fullName()
								+ " ("
								+ QString::number(QDate::currentDate()
										  .daysTo(destdate))
								+ " Days) </b></font><br>";
					}
				}
			} else {
				output = "<font color=" + m_headlineColor + ">"
					+ QObject::tr( "No birthdays in <b> %1 </b> days!" )
					.arg( m_daysLookAhead )
					+ "</font> <br>";
			}
		}
    }

	if ( m_showAnniversaries ){
		// Define the query for anniversaries and start search..
		Opie::OPimContact queryanniversaries;
		queryanniversaries.setAnniversary( lookAheadDate );

		m_list = m_contactdb->queryByExample( queryanniversaries, Opie::OPimContactAccess::DateDiff );

		ammount = 0;
		if ( m_list.count() > 0 ){
			output += "<font color=" + m_headlineColor + ">"
				+ QObject::tr( "Next anniversaries in <b> %1 </b> days:" )
				.arg( m_daysLookAhead )
				+ "</font> <br>";
			for ( m_it = m_list.begin(); m_it != m_list.end(); ++m_it ) {
				if ( ammount++ < m_maxLinesTask ){
					// Now we want to calculate how many days until anniversary.
					// We have to set the correct year to calculate the day diff...
					QDate destdate = (*m_it).anniversary();
					destdate.setYMD( QDate::currentDate().year(), destdate.month(),
							 destdate.day() );
					if ( QDate::currentDate().daysTo(destdate) < 0 )
						destdate.setYMD( QDate::currentDate().year()+1,
								 destdate.month(), destdate.day() );

					if ( QDate::currentDate().daysTo(destdate) < m_urgentDays )
						output += "<font color=" + m_urgentColor + "><b>-"
							+ (*m_it).fullName()
							+ " ("
							+ QString::number(QDate::currentDate()
									  .daysTo( destdate ) )
							+ " Days) </b></font><br>";
					else
						output += "<font color=" + m_entryColor + "><b>-"
							+ (*m_it).fullName()
							+ " ("
							+ QString::number(QDate::currentDate()
									  .daysTo( destdate ) )
							+ " Days) </b></font><br>";
				}
			}
		} else {
			output += "<font color=" + m_headlineColor + ">"
				+ QObject::tr( "No anniversaries in <b> %1 </b> days!" )
				.arg( m_daysLookAhead )
				+ "</font> <br>";
		}
	}

	addressLabel->setText( output );
}

/**
 * start the todolist
 */
void AddressBookPluginWidget::startAddressBook() {
    QCopEnvelope e( "QPE/System", "execute(QString)" );
    e << QString( "addressbook" );
}