~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kalarm/find.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  find.cpp  -  search facility
3
3
 *  Program:  kalarm
4
 
 *  Copyright © 2005-2009 by David Jarvie <djarvie@kde.org>
 
4
 *  Copyright © 2005-2010 by David Jarvie <djarvie@kde.org>
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
19
19
 */
20
20
 
21
21
#include "kalarm.h"
 
22
#include "find.moc"
22
23
 
23
 
#include <QGroupBox>
24
 
#include <QCheckBox>
25
 
#include <QVBoxLayout>
26
 
#include <QGridLayout>
 
24
#ifndef USE_AKONADI
 
25
#include "alarmlistfiltermodel.h"
 
26
#endif
 
27
#include "alarmlistview.h"
 
28
#include "eventlistview.h"
 
29
#include "kaevent.h"
 
30
#include "preferences.h"
27
31
 
28
32
#include <kfinddialog.h>
29
33
#include <kfind.h>
33
37
#include <kmessagebox.h>
34
38
#include <kdebug.h>
35
39
 
36
 
#include "alarmlistfiltermodel.h"
37
 
#include "alarmlistview.h"
38
 
#include "eventlistview.h"
39
 
#include "kaevent.h"
40
 
#include "preferences.h"
41
 
#include "find.moc"
 
40
#include <QGroupBox>
 
41
#include <QCheckBox>
 
42
#include <QVBoxLayout>
 
43
#include <QGridLayout>
42
44
 
43
45
// KAlarm-specific options for Find dialog
44
46
enum {
45
 
        FIND_LIVE     = KFind::MinimumUserOption,
46
 
        FIND_ARCHIVED = KFind::MinimumUserOption << 1,
47
 
        FIND_MESSAGE  = KFind::MinimumUserOption << 2,
48
 
        FIND_FILE     = KFind::MinimumUserOption << 3,
49
 
        FIND_COMMAND  = KFind::MinimumUserOption << 4,
50
 
        FIND_EMAIL    = KFind::MinimumUserOption << 5,
51
 
        FIND_AUDIO    = KFind::MinimumUserOption << 6
 
47
    FIND_LIVE     = KFind::MinimumUserOption,
 
48
    FIND_ARCHIVED = KFind::MinimumUserOption << 1,
 
49
    FIND_MESSAGE  = KFind::MinimumUserOption << 2,
 
50
    FIND_FILE     = KFind::MinimumUserOption << 3,
 
51
    FIND_COMMAND  = KFind::MinimumUserOption << 4,
 
52
    FIND_EMAIL    = KFind::MinimumUserOption << 5,
 
53
    FIND_AUDIO    = KFind::MinimumUserOption << 6
52
54
};
53
55
static long FIND_KALARM_OPTIONS = FIND_LIVE | FIND_ARCHIVED | FIND_MESSAGE | FIND_FILE | FIND_COMMAND | FIND_EMAIL | FIND_AUDIO;
54
56
 
55
57
 
56
58
class FindDlg : public KFindDialog
57
59
{
58
 
        public:
59
 
                FindDlg(QWidget* parent, long options = 0, const QStringList& findStrings = QStringList(), bool hasSelection = false)
60
 
                       : KFindDialog(parent, options, findStrings, hasSelection) {}
61
 
        protected slots:
62
 
                void slotButtonClicked(int button)
63
 
                {
64
 
                        if (button == Ok)
65
 
                                emit okClicked();
66
 
                        else
67
 
                                KFindDialog::slotButtonClicked(button);
68
 
                }
 
60
    public:
 
61
        FindDlg(QWidget* parent, long options = 0, const QStringList& findStrings = QStringList(), bool hasSelection = false)
 
62
            : KFindDialog(parent, options, findStrings, hasSelection) {}
 
63
    protected slots:
 
64
        void slotButtonClicked(int button)
 
65
        {
 
66
            if (button == Ok)
 
67
                emit okClicked();
 
68
            else
 
69
                KFindDialog::slotButtonClicked(button);
 
70
        }
69
71
};
70
72
 
71
73
 
72
74
Find::Find(EventListView* parent)
73
 
        : QObject(parent),
74
 
          mListView(parent),
75
 
          mDialog(0),
76
 
          mFind(0),
77
 
          mOptions(0)
 
75
    : QObject(parent),
 
76
      mListView(parent),
 
77
      mDialog(0),
 
78
      mFind(0),
 
79
      mOptions(0)
78
80
{
79
 
        connect(mListView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)), SLOT(slotSelectionChanged()));
 
81
    connect(mListView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)), SLOT(slotSelectionChanged()));
80
82
}
81
83
 
82
84
Find::~Find()
83
85
{
84
 
        delete mDialog;    // automatically set to 0
85
 
        delete mFind;
86
 
        mFind = 0;
 
86
    delete mDialog;    // automatically set to 0
 
87
    delete mFind;
 
88
    mFind = 0;
87
89
}
88
90
 
89
91
void Find::slotSelectionChanged()
90
92
{
91
 
        if (mDialog)
92
 
                mDialog->setHasCursor(mListView->selectionModel()->currentIndex().isValid());
 
93
    if (mDialog)
 
94
        mDialog->setHasCursor(mListView->selectionModel()->currentIndex().isValid());
93
95
}
94
96
 
95
97
/******************************************************************************
97
99
*/
98
100
void Find::display()
99
101
{
100
 
        if (!mOptions)
101
 
                // Set defaults the first time the Find dialog is activated
102
 
                mOptions = FIND_LIVE | FIND_ARCHIVED | FIND_MESSAGE | FIND_FILE | FIND_COMMAND | FIND_EMAIL | FIND_AUDIO;
103
 
        bool noArchived = !Preferences::archivedKeepDays();
104
 
        bool showArchived = qobject_cast<AlarmListView*>(mListView)
105
 
                            && (static_cast<AlarmListFilterModel*>(mListView->model())->statusFilter() & KCalEvent::ARCHIVED);
106
 
        if (noArchived  ||  !showArchived)      // these settings could change between activations
107
 
                mOptions &= ~FIND_ARCHIVED;
 
102
    if (!mOptions)
 
103
        // Set defaults the first time the Find dialog is activated
 
104
        mOptions = FIND_LIVE | FIND_ARCHIVED | FIND_MESSAGE | FIND_FILE | FIND_COMMAND | FIND_EMAIL | FIND_AUDIO;
 
105
    bool noArchived = !Preferences::archivedKeepDays();
 
106
    bool showArchived = qobject_cast<AlarmListView*>(mListView)
 
107
#ifdef USE_AKONADI
 
108
                        && (static_cast<AlarmListModel*>(mListView->model())->eventTypeFilter() & KAlarm::CalEvent::ARCHIVED);
 
109
#else
 
110
                        && (static_cast<AlarmListFilterModel*>(mListView->model())->statusFilter() & KAlarm::CalEvent::ARCHIVED);
 
111
#endif
 
112
    if (noArchived  ||  !showArchived)      // these settings could change between activations
 
113
        mOptions &= ~FIND_ARCHIVED;
108
114
 
109
 
        if (mDialog)
110
 
        {
 
115
    if (mDialog)
 
116
    {
111
117
#ifdef Q_WS_X11
112
 
                KWindowSystem::activateWindow(mDialog->winId());
113
 
#endif
114
 
        }
115
 
        else
116
 
        {
117
 
                mDialog = new FindDlg(mListView, mOptions, mHistory, (mListView->selectionModel()->selectedRows().count() > 1));
118
 
                mDialog->setModal(false);
119
 
                mDialog->setObjectName("FindDlg");
120
 
                mDialog->setHasSelection(false);
121
 
                QWidget* kalarmWidgets = mDialog->findExtension();
122
 
 
123
 
                // Alarm types
124
 
                QVBoxLayout* layout = new QVBoxLayout(kalarmWidgets);
125
 
                layout->setMargin(0);
126
 
                layout->setSpacing(KDialog::spacingHint());
127
 
                QGroupBox* group = new QGroupBox(i18nc("@title:group", "Alarm Type"), kalarmWidgets);
128
 
                layout->addWidget(group);
129
 
                QGridLayout* grid = new QGridLayout(group);
130
 
                grid->setMargin(KDialog::marginHint());
131
 
                grid->setSpacing(KDialog::spacingHint());
132
 
                grid->setColumnStretch(1, 1);
133
 
 
134
 
                // Live & archived alarm selection
135
 
                mLive = new QCheckBox(i18nc("@option:check Alarm type", "Active"), group);
136
 
                mLive->setFixedSize(mLive->sizeHint());
137
 
                mLive->setWhatsThis(i18nc("@info:whatsthis", "Check to include active alarms in the search."));
138
 
                grid->addWidget(mLive, 1, 0, Qt::AlignLeft);
139
 
 
140
 
                mArchived = new QCheckBox(i18nc("@option:check Alarm type", "Archived"), group);
141
 
                mArchived->setFixedSize(mArchived->sizeHint());
142
 
                mArchived->setWhatsThis(i18nc("@info:whatsthis", "Check to include archived alarms in the search. "
143
 
                                             "This option is only available if archived alarms are currently being displayed."));
144
 
                grid->addWidget(mArchived, 1, 2, Qt::AlignLeft);
145
 
 
146
 
                mActiveArchivedSep = new KSeparator(Qt::Horizontal, kalarmWidgets);
147
 
                grid->addWidget(mActiveArchivedSep, 2, 0, 1, 3);
148
 
 
149
 
                // Alarm actions
150
 
                mMessageType = new QCheckBox(i18nc("@option:check Alarm action = text display", "Text"), group);
151
 
                mMessageType->setFixedSize(mMessageType->sizeHint());
152
 
                mMessageType->setWhatsThis(i18nc("@info:whatsthis", "Check to include text message alarms in the search."));
153
 
                grid->addWidget(mMessageType, 3, 0);
154
 
 
155
 
                mFileType = new QCheckBox(i18nc("@option:check Alarm action = file display", "File"), group);
156
 
                mFileType->setFixedSize(mFileType->sizeHint());
157
 
                mFileType->setWhatsThis(i18nc("@info:whatsthis", "Check to include file alarms in the search."));
158
 
                grid->addWidget(mFileType, 3, 2);
159
 
 
160
 
                mCommandType = new QCheckBox(i18nc("@option:check Alarm action", "Command"), group);
161
 
                mCommandType->setFixedSize(mCommandType->sizeHint());
162
 
                mCommandType->setWhatsThis(i18nc("@info:whatsthis", "Check to include command alarms in the search."));
163
 
                grid->addWidget(mCommandType, 4, 0);
164
 
 
165
 
                mEmailType = new QCheckBox(i18nc("@option:check Alarm action", "Email"), group);
166
 
                mEmailType->setFixedSize(mEmailType->sizeHint());
167
 
                mEmailType->setWhatsThis(i18nc("@info:whatsthis", "Check to include email alarms in the search."));
168
 
                grid->addWidget(mEmailType, 4, 2);
169
 
 
170
 
                mAudioType = new QCheckBox(i18nc("@option:check Alarm action", "Audio"), group);
171
 
                mAudioType->setFixedSize(mAudioType->sizeHint());
172
 
                mAudioType->setWhatsThis(i18nc("@info:whatsthis", "Check to include audio alarms in the search."));
173
 
                grid->addWidget(mAudioType, 5, 0);
174
 
 
175
 
                // Set defaults
176
 
                mLive->setChecked(mOptions & FIND_LIVE);
177
 
                mArchived->setChecked(mOptions & FIND_ARCHIVED);
178
 
                mMessageType->setChecked(mOptions & FIND_MESSAGE);
179
 
                mFileType->setChecked(mOptions & FIND_FILE);
180
 
                mCommandType->setChecked(mOptions & FIND_COMMAND);
181
 
                mEmailType->setChecked(mOptions & FIND_EMAIL);
182
 
                mAudioType->setChecked(mOptions & FIND_AUDIO);
183
 
 
184
 
                connect(mDialog, SIGNAL(okClicked()), this, SLOT(slotFind()));
185
 
        }
186
 
 
187
 
        // Only display active/archived options if archived alarms are being kept
188
 
        if (noArchived)
189
 
        {
190
 
                mLive->hide();
191
 
                mArchived->hide();
192
 
                mActiveArchivedSep->hide();
193
 
        }
194
 
        else
195
 
        {
196
 
                mLive->show();
197
 
                mArchived->show();
198
 
                mActiveArchivedSep->show();
199
 
        }
200
 
 
201
 
        // Disable options where no displayed alarms match them
202
 
        bool live     = false;
203
 
        bool archived = false;
204
 
        bool text     = false;
205
 
        bool file     = false;
206
 
        bool command  = false;
207
 
        bool email    = false;
208
 
        bool audio    = false;
209
 
        int rowCount = mListView->model()->rowCount();
210
 
        for (int row = 0;  row < rowCount;  ++row)
211
 
        {
212
 
                const KAEvent* event = mListView->event(row);
213
 
                if (event->expired())
214
 
                        archived = true;
215
 
                else
216
 
                        live = true;
217
 
                switch (event->action())
218
 
                {
219
 
                        case KAEventData::MESSAGE:  text    = true;  break;
220
 
                        case KAEventData::FILE:     file    = true;  break;
221
 
                        case KAEventData::COMMAND:  command = true;  break;
222
 
                        case KAEventData::EMAIL:    email   = true;  break;
223
 
                        case KAEventData::AUDIO:    audio   = true;  break;
224
 
                }
225
 
        }
226
 
        mLive->setEnabled(live);
227
 
        mArchived->setEnabled(archived);
228
 
        mMessageType->setEnabled(text);
229
 
        mFileType->setEnabled(file);
230
 
        mCommandType->setEnabled(command);
231
 
        mEmailType->setEnabled(email);
232
 
        mAudioType->setEnabled(audio);
233
 
 
234
 
        mDialog->setHasCursor(mListView->selectionModel()->currentIndex().isValid());
235
 
        mDialog->show();
 
118
        KWindowSystem::activateWindow(mDialog->winId());
 
119
#endif
 
120
    }
 
121
    else
 
122
    {
 
123
        mDialog = new FindDlg(mListView, mOptions, mHistory, (mListView->selectionModel()->selectedRows().count() > 1));
 
124
        mDialog->setModal(false);
 
125
        mDialog->setObjectName("FindDlg");
 
126
        mDialog->setHasSelection(false);
 
127
        QWidget* kalarmWidgets = mDialog->findExtension();
 
128
 
 
129
        // Alarm types
 
130
        QVBoxLayout* layout = new QVBoxLayout(kalarmWidgets);
 
131
        layout->setMargin(0);
 
132
        layout->setSpacing(KDialog::spacingHint());
 
133
        QGroupBox* group = new QGroupBox(i18nc("@title:group", "Alarm Type"), kalarmWidgets);
 
134
        layout->addWidget(group);
 
135
        QGridLayout* grid = new QGridLayout(group);
 
136
        grid->setMargin(KDialog::marginHint());
 
137
        grid->setSpacing(KDialog::spacingHint());
 
138
        grid->setColumnStretch(1, 1);
 
139
 
 
140
        // Live & archived alarm selection
 
141
        mLive = new QCheckBox(i18nc("@option:check Alarm type", "Active"), group);
 
142
        mLive->setFixedSize(mLive->sizeHint());
 
143
        mLive->setWhatsThis(i18nc("@info:whatsthis", "Check to include active alarms in the search."));
 
144
        grid->addWidget(mLive, 1, 0, Qt::AlignLeft);
 
145
 
 
146
        mArchived = new QCheckBox(i18nc("@option:check Alarm type", "Archived"), group);
 
147
        mArchived->setFixedSize(mArchived->sizeHint());
 
148
        mArchived->setWhatsThis(i18nc("@info:whatsthis", "Check to include archived alarms in the search. "
 
149
                                     "This option is only available if archived alarms are currently being displayed."));
 
150
        grid->addWidget(mArchived, 1, 2, Qt::AlignLeft);
 
151
 
 
152
        mActiveArchivedSep = new KSeparator(Qt::Horizontal, kalarmWidgets);
 
153
        grid->addWidget(mActiveArchivedSep, 2, 0, 1, 3);
 
154
 
 
155
        // Alarm actions
 
156
        mMessageType = new QCheckBox(i18nc("@option:check Alarm action = text display", "Text"), group);
 
157
        mMessageType->setFixedSize(mMessageType->sizeHint());
 
158
        mMessageType->setWhatsThis(i18nc("@info:whatsthis", "Check to include text message alarms in the search."));
 
159
        grid->addWidget(mMessageType, 3, 0);
 
160
 
 
161
        mFileType = new QCheckBox(i18nc("@option:check Alarm action = file display", "File"), group);
 
162
        mFileType->setFixedSize(mFileType->sizeHint());
 
163
        mFileType->setWhatsThis(i18nc("@info:whatsthis", "Check to include file alarms in the search."));
 
164
        grid->addWidget(mFileType, 3, 2);
 
165
 
 
166
        mCommandType = new QCheckBox(i18nc("@option:check Alarm action", "Command"), group);
 
167
        mCommandType->setFixedSize(mCommandType->sizeHint());
 
168
        mCommandType->setWhatsThis(i18nc("@info:whatsthis", "Check to include command alarms in the search."));
 
169
        grid->addWidget(mCommandType, 4, 0);
 
170
 
 
171
        mEmailType = new QCheckBox(i18nc("@option:check Alarm action", "Email"), group);
 
172
        mEmailType->setFixedSize(mEmailType->sizeHint());
 
173
        mEmailType->setWhatsThis(i18nc("@info:whatsthis", "Check to include email alarms in the search."));
 
174
        grid->addWidget(mEmailType, 4, 2);
 
175
 
 
176
        mAudioType = new QCheckBox(i18nc("@option:check Alarm action", "Audio"), group);
 
177
        mAudioType->setFixedSize(mAudioType->sizeHint());
 
178
        mAudioType->setWhatsThis(i18nc("@info:whatsthis", "Check to include audio alarms in the search."));
 
179
        grid->addWidget(mAudioType, 5, 0);
 
180
 
 
181
        // Set defaults
 
182
        mLive->setChecked(mOptions & FIND_LIVE);
 
183
        mArchived->setChecked(mOptions & FIND_ARCHIVED);
 
184
        mMessageType->setChecked(mOptions & FIND_MESSAGE);
 
185
        mFileType->setChecked(mOptions & FIND_FILE);
 
186
        mCommandType->setChecked(mOptions & FIND_COMMAND);
 
187
        mEmailType->setChecked(mOptions & FIND_EMAIL);
 
188
        mAudioType->setChecked(mOptions & FIND_AUDIO);
 
189
 
 
190
        connect(mDialog, SIGNAL(okClicked()), this, SLOT(slotFind()));
 
191
    }
 
192
 
 
193
    // Only display active/archived options if archived alarms are being kept
 
194
    if (noArchived)
 
195
    {
 
196
        mLive->hide();
 
197
        mArchived->hide();
 
198
        mActiveArchivedSep->hide();
 
199
    }
 
200
    else
 
201
    {
 
202
        mLive->show();
 
203
        mArchived->show();
 
204
        mActiveArchivedSep->show();
 
205
    }
 
206
 
 
207
    // Disable options where no displayed alarms match them
 
208
    bool live     = false;
 
209
    bool archived = false;
 
210
    bool text     = false;
 
211
    bool file     = false;
 
212
    bool command  = false;
 
213
    bool email    = false;
 
214
    bool audio    = false;
 
215
    int rowCount = mListView->model()->rowCount();
 
216
    for (int row = 0;  row < rowCount;  ++row)
 
217
    {
 
218
#ifdef USE_AKONADI
 
219
        KAEvent viewEvent = mListView->event(row);
 
220
        const KAEvent* event = &viewEvent;
 
221
#else
 
222
        const KAEvent* event = mListView->event(row);
 
223
#endif
 
224
        if (event->expired())
 
225
            archived = true;
 
226
        else
 
227
            live = true;
 
228
        switch (event->action())
 
229
        {
 
230
            case KAEvent::MESSAGE:  text    = true;  break;
 
231
            case KAEvent::FILE:     file    = true;  break;
 
232
            case KAEvent::COMMAND:  command = true;  break;
 
233
            case KAEvent::EMAIL:    email   = true;  break;
 
234
            case KAEvent::AUDIO:    audio   = true;  break;
 
235
        }
 
236
    }
 
237
    mLive->setEnabled(live);
 
238
    mArchived->setEnabled(archived);
 
239
    mMessageType->setEnabled(text);
 
240
    mFileType->setEnabled(file);
 
241
    mCommandType->setEnabled(command);
 
242
    mEmailType->setEnabled(email);
 
243
    mAudioType->setEnabled(audio);
 
244
 
 
245
    mDialog->setHasCursor(mListView->selectionModel()->currentIndex().isValid());
 
246
    mDialog->show();
236
247
}
237
248
 
238
249
/******************************************************************************
240
251
*/
241
252
void Find::slotFind()
242
253
{
243
 
        if (!mDialog)
244
 
                return;
245
 
        mHistory = mDialog->findHistory();    // save search history so that it can be displayed again
246
 
        mOptions = mDialog->options() & ~FIND_KALARM_OPTIONS;
247
 
        mOptions |= (mLive->isEnabled()        && mLive->isChecked()        ? FIND_LIVE : 0)
248
 
                 |  (mArchived->isEnabled()    && mArchived->isChecked()    ? FIND_ARCHIVED : 0)
249
 
                 |  (mMessageType->isEnabled() && mMessageType->isChecked() ? FIND_MESSAGE : 0)
250
 
                 |  (mFileType->isEnabled()    && mFileType->isChecked()    ? FIND_FILE : 0)
251
 
                 |  (mCommandType->isEnabled() && mCommandType->isChecked() ? FIND_COMMAND : 0)
252
 
                 |  (mEmailType->isEnabled()   && mEmailType->isChecked()   ? FIND_EMAIL : 0)
253
 
                 |  (mAudioType->isEnabled()   && mAudioType->isChecked()   ? FIND_AUDIO : 0);
254
 
        if (!(mOptions & (FIND_LIVE | FIND_ARCHIVED))
255
 
        ||  !(mOptions & (FIND_MESSAGE | FIND_FILE | FIND_COMMAND | FIND_EMAIL | FIND_AUDIO)))
256
 
        {
257
 
                KMessageBox::sorry(mDialog, i18nc("@info", "No alarm types are selected to search"));
258
 
                return;
259
 
        }
260
 
 
261
 
        // Supply KFind with only those options which relate to the text within alarms
262
 
        long options = mOptions & (KFind::WholeWordsOnly | KFind::CaseSensitive | KFind::RegularExpression);
263
 
        bool newFind = !mFind;
264
 
        bool newPattern = (mDialog->pattern() != mLastPattern);
265
 
        mLastPattern = mDialog->pattern();
266
 
        if (mFind)
267
 
        {
268
 
                mFind->resetCounts();
269
 
                mFind->setPattern(mLastPattern);
270
 
                mFind->setOptions(options);
271
 
        }
272
 
        else
273
 
        {
274
 
                mFind = new KFind(mLastPattern, options, mListView, mDialog);
275
 
                connect(mFind, SIGNAL(destroyed()), SLOT(slotKFindDestroyed()));
276
 
                mFind->closeFindNextDialog();    // prevent 'Find Next' dialog appearing
277
 
        }
278
 
 
279
 
        // Set the starting point for the search
280
 
        mStartID.clear();
281
 
        mNoCurrentItem = newPattern;
282
 
        bool checkEnd = false;
283
 
        if (newPattern)
284
 
        {
285
 
                mFound = false;
286
 
                if (mOptions & KFind::FromCursor)
287
 
                {
288
 
                        QModelIndex index = mListView->selectionModel()->currentIndex();
289
 
                        if (index.isValid())
290
 
                        {
291
 
                                mStartID       = mListView->event(index)->id();
292
 
                                mNoCurrentItem = false;
293
 
                                checkEnd = true;
294
 
                        }
295
 
                }
296
 
        }
297
 
 
298
 
        // Execute the search
299
 
        findNext(true, checkEnd, false);
300
 
        if (mFind  &&  newFind)
301
 
                emit active(true);
 
254
    if (!mDialog)
 
255
        return;
 
256
    mHistory = mDialog->findHistory();    // save search history so that it can be displayed again
 
257
    mOptions = mDialog->options() & ~FIND_KALARM_OPTIONS;
 
258
    mOptions |= (mLive->isEnabled()        && mLive->isChecked()        ? FIND_LIVE : 0)
 
259
             |  (mArchived->isEnabled()    && mArchived->isChecked()    ? FIND_ARCHIVED : 0)
 
260
             |  (mMessageType->isEnabled() && mMessageType->isChecked() ? FIND_MESSAGE : 0)
 
261
             |  (mFileType->isEnabled()    && mFileType->isChecked()    ? FIND_FILE : 0)
 
262
             |  (mCommandType->isEnabled() && mCommandType->isChecked() ? FIND_COMMAND : 0)
 
263
             |  (mEmailType->isEnabled()   && mEmailType->isChecked()   ? FIND_EMAIL : 0)
 
264
             |  (mAudioType->isEnabled()   && mAudioType->isChecked()   ? FIND_AUDIO : 0);
 
265
    if (!(mOptions & (FIND_LIVE | FIND_ARCHIVED))
 
266
    ||  !(mOptions & (FIND_MESSAGE | FIND_FILE | FIND_COMMAND | FIND_EMAIL | FIND_AUDIO)))
 
267
    {
 
268
        KMessageBox::sorry(mDialog, i18nc("@info", "No alarm types are selected to search"));
 
269
        return;
 
270
    }
 
271
 
 
272
    // Supply KFind with only those options which relate to the text within alarms
 
273
    long options = mOptions & (KFind::WholeWordsOnly | KFind::CaseSensitive | KFind::RegularExpression);
 
274
    bool newFind = !mFind;
 
275
    bool newPattern = (mDialog->pattern() != mLastPattern);
 
276
    mLastPattern = mDialog->pattern();
 
277
    if (mFind)
 
278
    {
 
279
        mFind->resetCounts();
 
280
        mFind->setPattern(mLastPattern);
 
281
        mFind->setOptions(options);
 
282
    }
 
283
    else
 
284
    {
 
285
        mFind = new KFind(mLastPattern, options, mListView, mDialog);
 
286
        connect(mFind, SIGNAL(destroyed()), SLOT(slotKFindDestroyed()));
 
287
        mFind->closeFindNextDialog();    // prevent 'Find Next' dialog appearing
 
288
    }
 
289
 
 
290
    // Set the starting point for the search
 
291
    mStartID.clear();
 
292
    mNoCurrentItem = newPattern;
 
293
    bool checkEnd = false;
 
294
    if (newPattern)
 
295
    {
 
296
        mFound = false;
 
297
        if (mOptions & KFind::FromCursor)
 
298
        {
 
299
            QModelIndex index = mListView->selectionModel()->currentIndex();
 
300
            if (index.isValid())
 
301
            {
 
302
#ifdef USE_AKONADI
 
303
                mStartID       = mListView->event(index).id();
 
304
#else
 
305
                mStartID       = mListView->event(index)->id();
 
306
#endif
 
307
                mNoCurrentItem = false;
 
308
                checkEnd = true;
 
309
            }
 
310
        }
 
311
    }
 
312
 
 
313
    // Execute the search
 
314
    findNext(true, checkEnd, false);
 
315
    if (mFind  &&  newFind)
 
316
        emit active(true);
302
317
}
303
318
 
304
319
/******************************************************************************
308
323
*/
309
324
void Find::findNext(bool forward, bool checkEnd, bool fromCurrent)
310
325
{
311
 
        QModelIndex index;
312
 
        if (!mNoCurrentItem)
313
 
                index = mListView->selectionModel()->currentIndex();
314
 
        if (!fromCurrent)
315
 
                index = nextItem(index, forward);
316
 
 
317
 
        // Search successive alarms until a match is found or the end is reached
318
 
        bool found = false;
319
 
        bool last = false;
320
 
        for ( ;  index.isValid() && !last;  index = nextItem(index, forward))
321
 
        {
322
 
                const KAEvent* event = mListView->event(index);
323
 
                if (!fromCurrent  &&  !mStartID.isNull()  &&  mStartID == event->id())
324
 
                        last = true;    // we've wrapped round and reached the starting alarm again
325
 
                fromCurrent = false;
326
 
                bool live = !event->expired();
327
 
                if ((live  &&  !(mOptions & FIND_LIVE))
328
 
                ||  (!live  &&  !(mOptions & FIND_ARCHIVED)))
329
 
                        continue;     // we're not searching this type of alarm
330
 
                switch (event->action())
331
 
                {
332
 
                        case KAEventData::MESSAGE:
333
 
                                if (!(mOptions & FIND_MESSAGE))
334
 
                                        break;
335
 
                                mFind->setData(event->cleanText());
336
 
                                found = (mFind->find() == KFind::Match);
337
 
                                break;
338
 
 
339
 
                        case KAEventData::FILE:
340
 
                                if (!(mOptions & FIND_FILE))
341
 
                                        break;
342
 
                                mFind->setData(event->cleanText());
343
 
                                found = (mFind->find() == KFind::Match);
344
 
                                break;
345
 
 
346
 
                        case KAEventData::COMMAND:
347
 
                                if (!(mOptions & FIND_COMMAND))
348
 
                                        break;
349
 
                                mFind->setData(event->cleanText());
350
 
                                found = (mFind->find() == KFind::Match);
351
 
                                break;
352
 
 
353
 
                        case KAEventData::EMAIL:
354
 
                                if (!(mOptions & FIND_EMAIL))
355
 
                                        break;
356
 
                                mFind->setData(event->emailAddresses(", "));
357
 
                                found = (mFind->find() == KFind::Match);
358
 
                                if (found)
359
 
                                        break;
360
 
                                mFind->setData(event->emailSubject());
361
 
                                found = (mFind->find() == KFind::Match);
362
 
                                if (found)
363
 
                                        break;
364
 
                                mFind->setData(event->emailAttachments().join(", "));
365
 
                                found = (mFind->find() == KFind::Match);
366
 
                                if (found)
367
 
                                        break;
368
 
                                mFind->setData(event->cleanText());
369
 
                                found = (mFind->find() == KFind::Match);
370
 
                                break;
371
 
 
372
 
                        case KAEventData::AUDIO:
373
 
                                if (!(mOptions & FIND_AUDIO))
374
 
                                        break;
375
 
                                mFind->setData(event->audioFile());
376
 
                                found = (mFind->find() == KFind::Match);
377
 
                                break;
378
 
                }
379
 
                if (found)
380
 
                        break;
381
 
        }
382
 
 
383
 
        // Process the search result
384
 
        mNoCurrentItem = !index.isValid();
385
 
        if (found)
386
 
        {
387
 
                // A matching alarm was found - highlight it and make it current
388
 
                mFound = true;
389
 
                QItemSelectionModel* sel = mListView->selectionModel();
390
 
                sel->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
391
 
                sel->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
392
 
                mListView->scrollTo(index);
393
 
        }
394
 
        else
395
 
        {
396
 
                // No match was found
397
 
                if (mFound  ||  checkEnd)
398
 
                {
399
 
                        QString msg = forward ? i18nc("@info", "<para>End of alarm list reached.</para><para>Continue from the beginning?</para>")
400
 
                                              : i18nc("@info", "<para>Beginning of alarm list reached.</para><para>Continue from the end?</para>");
401
 
                        if (KMessageBox::questionYesNo(mListView, msg, QString(), KStandardGuiItem::cont(), KStandardGuiItem::cancel()) == KMessageBox::Yes)
402
 
                        {
403
 
                                mNoCurrentItem = true;
404
 
                                findNext(forward, false, false);
405
 
                                return;
406
 
                        }
407
 
                }
408
 
                else
409
 
                        mFind->displayFinalDialog();     // display "no match was found"
410
 
                mNoCurrentItem = false;    // restart from the currently highlighted alarm if Find Next etc selected
411
 
        }
 
326
    QModelIndex index;
 
327
    if (!mNoCurrentItem)
 
328
        index = mListView->selectionModel()->currentIndex();
 
329
    if (!fromCurrent)
 
330
        index = nextItem(index, forward);
 
331
 
 
332
    // Search successive alarms until a match is found or the end is reached
 
333
    bool found = false;
 
334
    bool last = false;
 
335
    for ( ;  index.isValid() && !last;  index = nextItem(index, forward))
 
336
    {
 
337
#ifdef USE_AKONADI
 
338
        KAEvent viewEvent = mListView->event(index);
 
339
        const KAEvent* event = &viewEvent;
 
340
#else
 
341
        const KAEvent* event = mListView->event(index);
 
342
#endif
 
343
        if (!fromCurrent  &&  !mStartID.isNull()  &&  mStartID == event->id())
 
344
            last = true;    // we've wrapped round and reached the starting alarm again
 
345
        fromCurrent = false;
 
346
        bool live = !event->expired();
 
347
        if ((live  &&  !(mOptions & FIND_LIVE))
 
348
        ||  (!live  &&  !(mOptions & FIND_ARCHIVED)))
 
349
            continue;     // we're not searching this type of alarm
 
350
        switch (event->action())
 
351
        {
 
352
            case KAEvent::MESSAGE:
 
353
                if (!(mOptions & FIND_MESSAGE))
 
354
                    break;
 
355
                mFind->setData(event->cleanText());
 
356
                found = (mFind->find() == KFind::Match);
 
357
                break;
 
358
 
 
359
            case KAEvent::FILE:
 
360
                if (!(mOptions & FIND_FILE))
 
361
                    break;
 
362
                mFind->setData(event->cleanText());
 
363
                found = (mFind->find() == KFind::Match);
 
364
                break;
 
365
 
 
366
            case KAEvent::COMMAND:
 
367
                if (!(mOptions & FIND_COMMAND))
 
368
                    break;
 
369
                mFind->setData(event->cleanText());
 
370
                found = (mFind->find() == KFind::Match);
 
371
                break;
 
372
 
 
373
            case KAEvent::EMAIL:
 
374
                if (!(mOptions & FIND_EMAIL))
 
375
                    break;
 
376
                mFind->setData(event->emailAddresses(", "));
 
377
                found = (mFind->find() == KFind::Match);
 
378
                if (found)
 
379
                    break;
 
380
                mFind->setData(event->emailSubject());
 
381
                found = (mFind->find() == KFind::Match);
 
382
                if (found)
 
383
                    break;
 
384
                mFind->setData(event->emailAttachments().join(", "));
 
385
                found = (mFind->find() == KFind::Match);
 
386
                if (found)
 
387
                    break;
 
388
                mFind->setData(event->cleanText());
 
389
                found = (mFind->find() == KFind::Match);
 
390
                break;
 
391
 
 
392
            case KAEvent::AUDIO:
 
393
                if (!(mOptions & FIND_AUDIO))
 
394
                    break;
 
395
                mFind->setData(event->audioFile());
 
396
                found = (mFind->find() == KFind::Match);
 
397
                break;
 
398
        }
 
399
        if (found)
 
400
            break;
 
401
    }
 
402
 
 
403
    // Process the search result
 
404
    mNoCurrentItem = !index.isValid();
 
405
    if (found)
 
406
    {
 
407
        // A matching alarm was found - highlight it and make it current
 
408
        mFound = true;
 
409
        QItemSelectionModel* sel = mListView->selectionModel();
 
410
        sel->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
 
411
        sel->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
 
412
        mListView->scrollTo(index);
 
413
    }
 
414
    else
 
415
    {
 
416
        // No match was found
 
417
        if (mFound  ||  checkEnd)
 
418
        {
 
419
            QString msg = forward ? i18nc("@info", "<para>End of alarm list reached.</para><para>Continue from the beginning?</para>")
 
420
                                  : i18nc("@info", "<para>Beginning of alarm list reached.</para><para>Continue from the end?</para>");
 
421
            if (KMessageBox::questionYesNo(mListView, msg, QString(), KStandardGuiItem::cont(), KStandardGuiItem::cancel()) == KMessageBox::Yes)
 
422
            {
 
423
                mNoCurrentItem = true;
 
424
                findNext(forward, false, false);
 
425
                return;
 
426
            }
 
427
        }
 
428
        else
 
429
            mFind->displayFinalDialog();     // display "no match was found"
 
430
        mNoCurrentItem = false;    // restart from the currently highlighted alarm if Find Next etc selected
 
431
    }
412
432
}
413
433
 
414
434
/******************************************************************************
416
436
*/
417
437
QModelIndex Find::nextItem(const QModelIndex& index, bool forward) const
418
438
{
419
 
        if (mOptions & KFind::FindBackwards)
420
 
                forward = !forward;
421
 
        if (!index.isValid())
422
 
        {
423
 
                QAbstractItemModel* model = mListView->model();
424
 
                if (forward)
425
 
                        return model->index(0, 0);
426
 
                else
427
 
                        return model->index(model->rowCount() - 1, 0);
428
 
        }
429
 
        if (forward)
430
 
                return mListView->indexBelow(index);
431
 
        else
432
 
                return mListView->indexAbove(index);
 
439
    if (mOptions & KFind::FindBackwards)
 
440
        forward = !forward;
 
441
    if (!index.isValid())
 
442
    {
 
443
        QAbstractItemModel* model = mListView->model();
 
444
        if (forward)
 
445
            return model->index(0, 0);
 
446
        else
 
447
            return model->index(model->rowCount() - 1, 0);
 
448
    }
 
449
    if (forward)
 
450
        return mListView->indexBelow(index);
 
451
    else
 
452
        return mListView->indexAbove(index);
433
453
}
 
454
 
 
455
// vim: et sw=4: