~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to src/plugins/generic/urlwatcher/urlwatcherplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <QString>
29
29
 
30
30
#include "psiplugin.h"
 
31
#include "eventfilter.h"
31
32
#include "urlevent.h"
32
33
 
33
34
 
34
 
class URLWatcherPlugin : public QObject, public PsiPlugin
 
35
class URLWatcherPlugin : public QObject, public PsiPlugin, public EventFilter
35
36
{
36
37
        Q_OBJECT
37
 
        Q_INTERFACES(PsiPlugin)
 
38
        Q_INTERFACES(PsiPlugin EventFilter)
38
39
 
39
40
public:
40
41
        URLWatcherPlugin();
41
42
        ~URLWatcherPlugin();
42
 
        virtual QString name() const; 
 
43
 
 
44
        virtual QString name() const;
43
45
        virtual QString shortName() const;
44
 
        virtual void message( const QString& message, const QString& fromJid, const QString& fromDisplay); 
 
46
        virtual QString version() const;
 
47
        virtual QWidget* options() const;
 
48
        virtual bool enable();
 
49
        virtual bool disable();
 
50
 
 
51
    virtual bool processEvent(int account, const QDomElement& e);
 
52
        virtual bool processMessage(int account, const QString& fromJid, const QString& body, const QString& subject) ;
45
53
 
46
54
private:
 
55
        bool enabled_;
47
56
        QList<URLEvent> urls_;
48
57
        QWidget* viewer_;
49
58
        QTextEdit* viewerText_;
53
62
 
54
63
URLWatcherPlugin::URLWatcherPlugin()
55
64
{
 
65
        enabled_ = false;
56
66
        viewer_ = new QWidget();
57
67
        QVBoxLayout *vboxLayout;
58
68
        QFrame *frame;
74
84
        label->setText("URLs");
75
85
        vboxLayout->addWidget(label);
76
86
        vboxLayout->addWidget(viewerText_);
77
 
        viewer_->show();
 
87
        //viewer_->show();
78
88
}
79
89
 
80
90
URLWatcherPlugin::~URLWatcherPlugin()
92
102
        return "urlWatcher";
93
103
}
94
104
 
 
105
QString URLWatcherPlugin::version() const
 
106
{
 
107
        return "0.2";
 
108
}
 
109
 
 
110
QWidget* URLWatcherPlugin::options() const
 
111
{
 
112
        return 0;
 
113
}
 
114
 
 
115
bool URLWatcherPlugin::enable()
 
116
{
 
117
        viewer_->show();
 
118
        enabled_ = true;
 
119
        return true;
 
120
}
 
121
 
 
122
bool URLWatcherPlugin::disable()
 
123
{
 
124
        viewer_->hide();
 
125
        enabled_ = false;
 
126
        return true;
 
127
}
 
128
 
95
129
QString resolveEntities(const QString &in)
96
130
{
97
131
        QString out;
129
163
static bool linkify_pmatch(const QString &str1, int at, const QString &str2)
130
164
{
131
165
        if(str2.length() > (str1.length()-at))
132
 
                return FALSE;
 
166
                return false;
133
167
 
134
168
        for(int n = 0; n < (int)str2.length(); ++n) {
135
169
                if(str1.at(n+at).toLower() != str2.at(n).toLower())
136
 
                        return FALSE;
 
170
                        return false;
137
171
        }
138
172
 
139
 
        return TRUE;
 
173
        return true;
140
174
}
141
175
 
142
176
static bool linkify_isOneOf(const QChar &c, const QString &charlist)
143
177
{
144
178
        for(int i = 0; i < (int)charlist.length(); ++i) {
145
179
                if(c == charlist.at(i))
146
 
                        return TRUE;
 
180
                        return true;
147
181
        }
148
182
 
149
 
        return FALSE;
 
183
        return false;
150
184
}
151
185
 
152
186
// encodes a few dangerous html characters
172
206
static bool linkify_okUrl(const QString &url)
173
207
{
174
208
        if(url.at(url.length()-1) == '.')
175
 
                return FALSE;
 
209
                return false;
176
210
 
177
 
        return TRUE;
 
211
        return true;
178
212
}
179
213
 
180
214
static bool linkify_okEmail(const QString &addy)
183
217
        // at least one char for each of the three sections
184
218
        int n = addy.indexOf('@');
185
219
        if(n == -1 || n == 0)
186
 
                return FALSE;
 
220
                return false;
187
221
        int d = addy.indexOf('.', n+1);
188
222
        if(d == -1 || d == 0)
189
 
                return FALSE;
 
223
                return false;
190
224
        if((addy.length()-1) - d <= 0)
191
 
                return FALSE;
 
225
                return false;
192
226
        if(addy.indexOf("..") != -1)
193
227
                return false;
194
228
 
195
 
        return TRUE;
196
 
}
197
 
 
198
 
void URLWatcherPlugin::message( const QString& message, const QString& fromJid, const QString& fromDisplay)
199
 
{
 
229
        return true;
 
230
}
 
231
 
 
232
bool URLWatcherPlugin::processEvent(int account, const QDomElement& e)
 
233
{
 
234
        Q_UNUSED(account);
 
235
        Q_UNUSED(e);
 
236
        return false;
 
237
}
 
238
 
 
239
 
 
240
bool URLWatcherPlugin::processMessage(int account, const QString& fromJid, const QString& body, const QString& subject)
 
241
{
 
242
        Q_UNUSED(account);
 
243
        Q_UNUSED(subject);
 
244
 
200
245
        QString newUrl;
201
 
        QString out = message;
 
246
        QString out = body;
202
247
        int x1, x2;
203
248
        bool isUrl, isEmail;
204
249
        QString linked, link, href;
205
250
 
206
251
        for(int n = 0; n < (int)out.length(); ++n) {
207
 
                isUrl = FALSE;
208
 
                isEmail = FALSE;
 
252
                isUrl = false;
 
253
                isEmail = false;
209
254
                x1 = n;
210
255
 
211
256
                if(linkify_pmatch(out, n, "http://")) {
212
257
                        n += 7;
213
 
                        isUrl = TRUE;
 
258
                        isUrl = true;
214
259
                        href = "";
215
260
                }
216
261
                else if(linkify_pmatch(out, n, "https://")) {
217
262
                        n += 8;
218
 
                        isUrl = TRUE;
 
263
                        isUrl = true;
219
264
                        href = "";
220
265
                }
221
266
                else if(linkify_pmatch(out, n, "ftp://")) {
222
267
                        n += 6;
223
 
                        isUrl = TRUE;
 
268
                        isUrl = true;
224
269
                        href = "";
225
270
                }
226
271
                else if(linkify_pmatch(out, n, "news://")) {
227
272
                        n += 7;
228
 
                        isUrl = TRUE;
 
273
                        isUrl = true;
229
274
                        href = "";
230
275
                }
231
276
                else if (linkify_pmatch(out, n, "ed2k://")) {
232
277
                        n += 7;
233
 
                        isUrl = TRUE;
 
278
                        isUrl = true;
234
279
                        href = "";
235
280
                }
236
281
                else if(linkify_pmatch(out, n, "www.")) {
237
 
                        isUrl = TRUE;
 
282
                        isUrl = true;
238
283
                        href = "http://";
239
284
                }
240
285
                else if(linkify_pmatch(out, n, "ftp.")) {
241
 
                        isUrl = TRUE;
 
286
                        isUrl = true;
242
287
                        href = "ftp://";
243
288
                }
244
289
                else if(linkify_pmatch(out, n, "@")) {
245
 
                        isEmail = TRUE;
 
290
                        isEmail = true;
246
291
                        href = "mailto:";
247
292
                }
248
293
 
319
364
        if (isUrl)
320
365
        {
321
366
                qWarning(qPrintable(QString("it's a url %1").arg(newUrl)));
322
 
                newUrl=QString("&lt;%1> <a href='%2'>%2</a><br />").arg(fromDisplay).arg(newUrl);
 
367
                newUrl=QString("&lt;%1> <a href='%2'>%2</a><br />").arg(fromJid).arg(newUrl);
323
368
                viewerText_->append(newUrl);
324
369
        }
 
370
 
 
371
        return false;
325
372
}       
326
373
 
327
 
#include "urlwatcherplugin.moc"
 
 
b'\\ No newline at end of file'
 
374
#include "urlwatcherplugin.moc"