code clean
[mdictionary] / src / plugins / xdxf / DownloadDict.h
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*!
23     \file DownloadDict.h
24     \brief Represenation of dictionary html entry on XDXF webpage
25
26     \author Bartosz Szatkowski <bulislaw@linux.com>
27 */
28
29 #ifndef DOWNLOADDICT_H
30 #define DOWNLOADDICT_H
31
32 #include <QRegExp>
33 #include <QStringList>
34 #include <math.h>
35
36 /*!
37     Each dictionary is representing as one html line at XDXF webpage,
38     each entry contains lang from, lang to, file name, file size, link, etc info
39     about dictionary. DownloadDict cut this description and then it may be
40     presented to the user and downloaded
41 */
42 class DownloadDict
43 {
44 public:
45
46     /*! Cuts html entry to class
47         Html entry looks like <tr><td>...</td><td>...</tr> some of the fields
48         dosn't matters for now so Iam ignoring it.
49         \param html html entry (line describing a dict) to be cut
50     */
51     DownloadDict(QString html) {
52         QRegExp reg("<td.*>(.*)</td>");
53         reg.setMinimal(true);
54         int pos = 0;
55         QStringList tmp;
56         while ((pos = reg.indexIn(html, pos)) != -1) {
57             tmp << reg.cap(1);
58             pos += reg.matchedLength();
59         }
60         _from = tmp.at(6);
61         _to = tmp.at(7);
62         _title = tmp.at(1);
63         QString sizeStr = tmp.at(3);
64
65         _size = sizeStr.remove(',').toInt();
66         _size = _size / 1024 / 1024;
67         _size = round(_size*1000) / 1000;
68
69         QRegExp lreg("href=\"(.*)\""); // Cutting link to file
70         lreg.setMinimal(true);
71         lreg.indexIn(tmp.at(2));
72         _link = lreg.capturedTexts().at(1);
73     }
74
75     DownloadDict(const DownloadDict &dict) {
76        _from = dict.fromLang();
77        _to = dict.toLang();
78        _title = dict.title();
79        _size = dict.size();
80        _link  = dict.link();
81     }
82
83     //! \return dictionary source language
84     QString fromLang() const {return _from;}
85
86     //! \return dictionary destination language
87     QString toLang() const {return _to;}
88
89     //! \return dictionary title
90     QString title() const {return _title;}
91  
92     //! \return dictionary archive size
93     float size() const {return _size;}
94
95     //! \return link to dictionary archive
96     QString link() const {return _link;}
97
98     /*!
99         Compares dict by from lang then to lang
100         \return 1 if this is before other
101     */
102     bool operator <(DownloadDict other) const {
103         if(_from < other.fromLang())
104             return true;
105         else if(_from > other.fromLang())
106             return false;
107         else if(_to < other.toLang())
108             return true;
109         else
110             return false;
111     }
112
113 private:
114     QString _from, _to, _title, _link;
115     float _size;
116 };
117
118
119 #endif // DOWNLOADDICT_H