summaryrefslogtreecommitdiff
path: root/core/pim/today/plugins/todolist/todopluginwidget.cpp
blob: 0364f9439f05e89bf8de42d674150ef8fcd3809f (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
/*
 * todopluginwidget.cpp
 *
 * copyright   : (c) 2002, 2003 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 "todopluginwidget.h"

#include <qpe/config.h>
#include <qpe/qcopenvelope_qws.h>

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

    todo = 0l;
    layoutTodo = 0l;
    todoLabel = 0l;

    if ( todo ) {
        delete todo;
    }
    todo = new OTodoAccess();
    todo->load();

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

    if ( todoLabel )  {
        delete todoLabel;
    }
    todoLabel = new OClickableLabel( this );

    connect( todoLabel, SIGNAL( clicked() ), this, SLOT( startTodolist() ) );

    readConfig();
    getTodo();
}

TodolistPluginWidget::~TodolistPluginWidget() {
    delete todo;
    delete todoLabel;
    delete layoutTodo;
}


void TodolistPluginWidget::readConfig() {
    Config cfg( "todaytodoplugin" );
    cfg.setGroup( "config" );
    m_maxLinesTask = cfg.readNumEntry( "maxlinestask", 5 );
    m_maxCharClip =  cfg.readNumEntry( "maxcharclip", 38 );
}

void TodolistPluginWidget:: refresh() {
    todo->reload();
    getTodo();
}

void TodolistPluginWidget::reinitialize() {
    readConfig();
    todo->reload();
    getTodo();
}

/**
 * Get the todos
 */
void TodolistPluginWidget::getTodo() {


    QString output;
    QString tmpout;
    int count = 0;
    int ammount = 0;

    // get overdue todos first
    m_list = todo->sorted( true, 3, 2, 1);

    for ( m_it = m_list.begin(); m_it != m_list.end(); ++m_it ) {
        if (!(*m_it).isCompleted() && ( ammount < m_maxLinesTask ) ) {
            QString desc = (*m_it).summary();
            if( desc.isEmpty() ) {
                desc = (*m_it).description();
            }
            tmpout += "<font color=#e00000><b>[" +  QString("%1").arg((*m_it).priority() )  + "]" + desc.mid( 0, m_maxCharClip ) + "</b></font><br>";
            ammount++ ;
        }
    }

    // get total number of still open todos
    m_list = todo->sorted( true, 1, 4, 1);

    for ( m_it = m_list.begin(); m_it != m_list.end(); ++m_it ) {
        count +=1;
        // not the overdues, we allready got them, and not if we are
        // over the maxlines
        if ( !(*m_it).isOverdue() && ( ammount < m_maxLinesTask ) ) {
            QString desc = (*m_it).summary();
            if( desc.isEmpty() ) {
                desc = (*m_it).description();
            }
            tmpout += "<b> [" +  QString("%1").arg((*m_it).priority() )  + "] </b>" + desc.mid( 0, m_maxCharClip ) + "<br>";
            ammount++;
        }
    }

    if ( count > 0 ) {
        if( count == 1 ) {
            output += QObject::tr( "There is <b> 1</b> active task:  <br>" );
        } else {
            output += QObject::tr( "There are <b> %1</b> active tasks: <br>" ).arg( count );
        }
        output += tmpout;
    } else {
        output = QObject::tr( "No active tasks" );
    }
    todoLabel->setText( output );
}

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