~ben-kietzman/ubuntu/quantal/mountmanager/fix-for-598070

« back to all changes in this revision

Viewing changes to plugins/Tips/tips.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2008-08-20 10:22:14 UTC
  • Revision ID: james.westby@ubuntu.com-20080820102214-fv93myu0ncb1503r
Tags: upstream-0.2.4
ImportĀ upstreamĀ versionĀ 0.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//MountManager - the program for easy mounting of storage devices in Linux
 
2
//Copyright (C) 2007-2008 Tikhonov Sergey
 
3
//
 
4
//This file is part of MountManager Plugin "Tips of the day"
 
5
//
 
6
//This program is free software; you can redistribute it and/or
 
7
//modify it under the terms of the GNU General Public License
 
8
//as published by the Free Software Foundation; either version 2
 
9
//of the License, or (at your option) any later version.
 
10
//
 
11
//This program is distributed in the hope that it will be useful,
 
12
//but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//GNU General Public License for more details.
 
15
//
 
16
//You should have received a copy of the GNU General Public License
 
17
//along with this program; if not, write to the Free Software
 
18
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
#include <QtGui/QDialog>
 
20
#include <QtGui/QAction>
 
21
#include <QtGui/QLabel>
 
22
#include <QtGui/QTextBrowser>
 
23
#include <QtGui/QCheckBox>
 
24
#include <QtGui/QPushButton>
 
25
#include <QtGui/QVBoxLayout>
 
26
#include <QtGui/QHBoxLayout>
 
27
#include <QtCore/QDir>
 
28
#include <QtCore/QFile>
 
29
#include <QtCore/QTextStream>
 
30
#include <QtCore/QSettings>
 
31
#include <QtPlugin>
 
32
#include "tips.h"
 
33
#include "../../sources/gui/const.h"
 
34
 
 
35
 
 
36
TipsDialog::TipsDialog(QWidget *parent) {
 
37
        QFile file(":/resources/tips.txt");
 
38
        file.open(QIODevice::ReadOnly);
 
39
        QTextStream stream(&file);
 
40
 
 
41
        QString line;
 
42
        int emptyLinesInRow = 0;
 
43
        QString currentTipText;
 
44
        while (!stream.atEnd()) {
 
45
                line = stream.readLine();
 
46
                if (line.isEmpty())
 
47
                        emptyLinesInRow++;
 
48
                else
 
49
                        emptyLinesInRow = 0;
 
50
                currentTipText += line;
 
51
                if (emptyLinesInRow == 2 || (!currentTipText.isEmpty() && stream.atEnd()))
 
52
                        if (!currentTipText.isEmpty()) {
 
53
                                tips.append(new Tip(currentTipText,tips.count()));
 
54
                                currentTipText.clear();
 
55
                        }
 
56
        }
 
57
        
 
58
        mainWidget = new QDialog(parent);
 
59
 
 
60
        textBrowser = new QTextBrowser;
 
61
 
 
62
        showOnStartup = new QCheckBox(tr("Show tips on startup"));
 
63
 
 
64
        nextButton = new QPushButton;
 
65
        nextButton->setIcon(QIcon(":/resources/next.png"));
 
66
        nextButton->setFlat(true);
 
67
        nextButton->setEnabled(tips.count() > 0);
 
68
        connect(nextButton,SIGNAL(clicked()),this,SLOT(next()));
 
69
        
 
70
        backButton = new QPushButton;
 
71
        backButton->setIcon(QIcon(":/resources/previous.png"));
 
72
        backButton->setFlat(true);
 
73
        backButton->setEnabled(false);
 
74
        connect(backButton,SIGNAL(clicked()),textBrowser,SLOT(backward()));
 
75
        connect(textBrowser,SIGNAL(backwardAvailable(bool)),backButton,SLOT(setEnabled(bool)));
 
76
 
 
77
        closeButton = new QPushButton;
 
78
        closeButton->setIcon(QIcon(":/resources/close.png"));
 
79
        closeButton->setFlat(true);
 
80
        connect(closeButton,SIGNAL(clicked()),mainWidget,SLOT(hide()));
 
81
 
 
82
        QLabel *headerLabel = new QLabel("<font size='4'><center><b>" + tr("Did you know?") + "</b></center></font>");
 
83
        
 
84
        QAction *action = new QAction(this);
 
85
        action->setText(tr("Tips of the day"));
 
86
        action->setIcon(QIcon(":/resources/main.png"));
 
87
        connect(action,SIGNAL(triggered()),this,SLOT(show()));
 
88
        actions.append(action);
 
89
 
 
90
        QHBoxLayout *buttonLayout = new QHBoxLayout;
 
91
        buttonLayout->addWidget(showOnStartup);
 
92
        buttonLayout->addStretch();
 
93
        buttonLayout->addWidget(backButton);
 
94
        buttonLayout->addWidget(nextButton);
 
95
        buttonLayout->addWidget(closeButton);
 
96
        buttonLayout->setMargin(0);
 
97
 
 
98
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
99
        mainLayout->addWidget(headerLabel);
 
100
        mainLayout->addWidget(textBrowser);
 
101
        mainLayout->addLayout(buttonLayout);
 
102
        
 
103
        mainWidget->setLayout(mainLayout);
 
104
        
 
105
        mainWidget->setWindowTitle(tr("Tips of the day"));
 
106
        mainWidget->setWindowIcon(QIcon(":/resources/main.png"));
 
107
        loadSettings();
 
108
}
 
109
 
 
110
TipsDialog::~TipsDialog() {
 
111
        foreach (QAction *action,actions)
 
112
                delete action;
 
113
        delete textBrowser;
 
114
        delete showOnStartup;
 
115
        delete nextButton;
 
116
        delete backButton;
 
117
        delete closeButton;
 
118
        delete mainWidget;
 
119
}
 
120
 
 
121
void TipsDialog::setParent(QObject *parent) {
 
122
        QSettings settings(ORGANIZATION,PROGRAM_NAME);
 
123
        if (settings.value("Tips/ShowOnStartup",true).toBool())
 
124
                connect(parent,SIGNAL(started()),this,SLOT(show()));
 
125
        connect(parent,SIGNAL(finished()),this,SLOT(saveSettings()));
 
126
}
 
127
 
 
128
const QString TipsDialog::pluginName() const {
 
129
        return tr("Tips of the day");
 
130
}
 
131
 
 
132
const QString TipsDialog::pluginDescription() const {
 
133
        return tr("You can read different advices for usage of MountManager and information about mounting in Linux.");
 
134
}
 
135
        
 
136
QDialog* TipsDialog::dialog() const {
 
137
        return mainWidget;
 
138
}
 
139
 
 
140
QDockWidget* TipsDialog::dockWidget() const {
 
141
        return 0;
 
142
}
 
143
 
 
144
QMenu* TipsDialog::menu() const {
 
145
        return 0;
 
146
}
 
147
 
 
148
QToolBar* TipsDialog::toolBar() const {
 
149
        return 0;
 
150
}
 
151
 
 
152
Actions TipsDialog::menuActions() const {
 
153
        return actions;
 
154
}
 
155
 
 
156
int TipsDialog::randomIndex() {
 
157
        if (watchedTipsIndexes.count() == tips.count())
 
158
                watchedTipsIndexes.clear();
 
159
        int randomNumber = (int)(double(tips.count())*rand()/(RAND_MAX+1.0));
 
160
        while (watchedTipsIndexes.contains(randomNumber))
 
161
                randomNumber = (int)(double(tips.count())*rand()/(RAND_MAX+1.0));
 
162
        return randomNumber;
 
163
}
 
164
 
 
165
void TipsDialog::show() {
 
166
        next();
 
167
        mainWidget->show();
 
168
}
 
169
 
 
170
void TipsDialog::next() {
 
171
        int index = randomIndex();
 
172
        watchedTipsIndexes.append(index);
 
173
        textBrowser->setHtml(tips[index]->tipContent);
 
174
}
 
175
 
 
176
void TipsDialog::saveSettings() {
 
177
        QSettings settings(ORGANIZATION,PROGRAM_NAME);
 
178
        settings.setValue("Tips/ShowOnStartup",showOnStartup->isChecked());
 
179
        settings.setValue("Tips/Size",mainWidget->size());
 
180
}
 
181
 
 
182
void TipsDialog::loadSettings() {
 
183
        QSettings settings(ORGANIZATION,PROGRAM_NAME);
 
184
        showOnStartup->setChecked(settings.value("Tips/ShowOnStartup",true).toBool());
 
185
        mainWidget->resize(settings.value("Tips/Size",QSize(450,300)).toSize());
 
186
}
 
187
 
 
188
Q_EXPORT_PLUGIN2(tipsplugin,TipsDialog)