summaryrefslogtreecommitdiff
path: root/core/pim/today/plugins/datebook/datebookpluginwidget.cpp
blob: b6707df78d3ad22b39d81289d63bc4639e863e20 (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
 /*
 * datebookpluginwidget.cpp
 *
 * copyright   : (c) 2002,2003, 2004 by Maximilian Reiß
 * email       : harlekin@handhelds.org
 *
 */
/***************************************************************************
 *                                                                         *
 *   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 "datebookpluginwidget.h"

#include <qpe/config.h>

#include <qtl.h>

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

    db = 0l;
    m_layoutDates = 0l;

    if ( m_layoutDates )  {
        delete m_layoutDates;
    }
    m_layoutDates = new QVBoxLayout( this );
    m_layoutDates->setAutoAdd( true );

    m_eventsList.setAutoDelete( true );

    readConfig();
    getDates();
}

DatebookPluginWidget::~DatebookPluginWidget() {
    delete db;
    delete m_layoutDates;
}


void DatebookPluginWidget::readConfig() {
    Config cfg( "todaydatebookplugin" );
    cfg.setGroup( "config" );
    m_max_lines_meet = cfg.readNumEntry( "maxlinesmeet", 5 );
    m_show_location = cfg.readNumEntry( "showlocation", 1 );
    m_show_notes = cfg.readNumEntry( "shownotes", 0 );
    m_onlyLater = cfg.readNumEntry( "onlylater", 1 );
    m_moreDays = cfg.readNumEntry( "moredays", 0 );
    m_timeExtraLine = cfg.readNumEntry( "timeextraline",  1 );
}

void DatebookPluginWidget::reinitialize()  {
    readConfig();
    refresh();
}

void DatebookPluginWidget::refresh()  {
    m_eventsList.clear();

    if ( m_layoutDates )  {
        delete m_layoutDates;
    }
    m_layoutDates = new QVBoxLayout( this );
    m_layoutDates->setAutoAdd( true );

    getDates();
}

/**
 *  Get all events that are in the datebook xml file for today
 */
void DatebookPluginWidget::getDates() {


    if ( db ) {
        delete db;
    }
    db = new DateBookDB;

    QDate date = QDate::currentDate();
    QValueList<EffectiveEvent> list = db->getEffectiveEvents( date, date.addDays( m_moreDays )  );
    qBubbleSort( list );
    int count = 0;

    if ( list.count() > 0 ) {

        for ( QValueList<EffectiveEvent>::ConstIterator it = list.begin(); it  != list.end(); ++it ) {

            if ( count < m_max_lines_meet ) {
                if ( !m_onlyLater ) {
                    count++;
                    DateBookEvent *l = new DateBookEvent( *it, this, m_show_location, m_show_notes, m_timeExtraLine );
                    m_eventsList.append( l );
                    l->show();
                    QObject::connect ( l, SIGNAL( editEvent( const Event & ) ), l, SLOT( editEventSlot( const Event & ) ) );
                } else {
			if ( ( QDateTime::currentDateTime()  <=  (*it).event().end() )
			     // Show events which span over many days and are not elapsed.
			     || ( ( (*it).event().start().date() != date ) &&  (  QDateTime::currentDateTime()  <=  (*it).event().end() )  )
			     // Show repeated event for today that is not elapsed.
 			     || ( ( (*it).event().repeatType() != Event::NoRepeat )
				  && ( ( date.dayOfWeek() == (*it).date().dayOfWeek() )
				       && ( QTime::currentTime() < (*it).event().start().time() ) ) )
			     // Show repeated event for next days.
			     || ( ( (*it).event().repeatType() != Event::NoRepeat )
				  && ( date.dayOfWeek() != (*it).date().dayOfWeek() ) )
			     )
			     {
				count++;
				// show only later appointments
				DateBookEvent *l = new DateBookEvent( *it, this, m_show_location, m_show_notes, m_timeExtraLine );
				m_eventsList.append( l );
				l->show();
				QObject::connect ( l, SIGNAL( editEvent( const Event & ) ), l, SLOT( editEventSlot( const Event & ) ) );
			     }
		}
            }
        }
        if ( m_onlyLater && count == 0 ) {
            QLabel* noMoreEvents = new QLabel( this );
            m_eventsList.append(  noMoreEvents );
            noMoreEvents->show();
            noMoreEvents->setText( QObject::tr( "No more appointments today" ) );
        }
    } else {
        QLabel* noEvents = new QLabel( this );
        m_eventsList.append( noEvents );
        noEvents->show();
        noEvents->setText( QObject::tr( "No appointments today" ) );
    }
}