change and add some comments, remove some warning
[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     \author Bartosz Szatkowski <bulislaw@linux.com>
26 */
27
28 #ifndef DOWNLOADDICT_H
29 #define DOWNLOADDICT_H
30
31 #include <QRegExp>
32 #include <QStringList>
33 #include <math.h>
34
35 /*!
36     Each dictionary is representing as one html line at XDXF webpage,
37     each entry contains lang from, lang to, file name, file size, link, etc info
38     about dictionary. DownloadDict cut this description and then it may be
39     presented to the user and downloaded
40 */
41 class DownloadDict
42 {
43 public:
44
45     /*! Cuts html entry to class
46         Html entry looks like <tr><td>...</td><td>...</tr> some of the fields
47         dosn't matters for now so Iam ignoring it.
48         \param html html entry (line describing a dict) to be cut
49     */
50     DownloadDict(QString html) {
51         QRegExp reg("<td.*>(.*)</td>");
52         reg.setMinimal(true);
53         int pos = 0;
54         QStringList tmp;
55         while ((pos = reg.indexIn(html, pos)) != -1) {
56             tmp << reg.cap(1);
57             pos += reg.matchedLength();
58         }
59         _from = tmp.at(6);
60         _to = tmp.at(7);
61         _title = tmp.at(1);
62         QString sizeStr = tmp.at(3);
63
64         _size = sizeStr.remove(',').toInt();
65         _size = _size / 1024 / 1024;
66         _size = round(_size*1000) / 1000;
67
68         QRegExp lreg("href=\"(.*)\""); // Cutting link to file
69         lreg.setMinimal(true);
70         lreg.indexIn(tmp.at(2));
71         _link = lreg.capturedTexts().at(1);
72     }
73
74     //! \return dictionary source language
75     QString fromLang() const {return _from;}
76
77     //! \return dictionary destination language
78     QString toLang() const {return _to;}
79
80     //! \return dictionary title
81     QString title() const {return _title;}
82  
83     //! \return dictionary archive size
84     float size() const {return _size;}
85
86     //! \return link to dictionary archive
87     QString link() const {return _link;}
88
89     /*!
90         Compares dict by from lang then to lang
91         \return 1 if this is before other
92     */
93     bool operator <(DownloadDict other) const {
94         if(_from < other.fromLang()) return true;
95         if(_from > other.fromLang()) return false;
96         if(_to < other.toLang()) return true;
97         return false;
98     }
99
100 private:
101     QString _from, _to, _title, _link;
102     float _size;
103 };
104
105
106 #endif // DOWNLOADDICT_H