cf6ff57467f72b091c61f7e9cc1693d52ad0daea
[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 /*! \file DownloadDict.h
23 \brief Represenation of dictionary html entry on XDXF webpage
24
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
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
50     \param html html entry (line describing a dict) to be cut
51     */
52     DownloadDict(QString html) {
53         QRegExp reg("<td.*>(.*)</td>");
54         reg.setMinimal(true);
55         int pos = 0;
56         QStringList tmp;
57         while ((pos = reg.indexIn(html, pos)) != -1) {
58             tmp << reg.cap(1);
59             pos += reg.matchedLength();
60         }
61         _from = tmp.at(6);
62         _to = tmp.at(7);
63         _title = tmp.at(1);
64         QString sizeStr = tmp.at(3);
65
66         _size = sizeStr.remove(',').toInt();
67
68         _size = _size / 1024 / 1024;
69
70         _size = round(_size*1000) / 1000;
71
72
73         QRegExp lreg("href=\"(.*)\""); // Cutting link to file
74         lreg.setMinimal(true);
75         lreg.indexIn(tmp.at(2));
76         _link = lreg.capturedTexts().at(1);
77     }
78
79     //! \return dictionary source language
80     QString fromLang() const {return _from;}
81
82     //! \return dictionary destination language
83     QString toLang() const {return _to;}
84
85     //! \return dictionary title
86     QString title() const {return _title;}
87  
88     //! \return dictionary archive size
89     float size() const {return _size;}
90
91
92     //! \return link to dictionary archive
93     QString link() const {return _link;}
94
95     //! Compares dict by from lang then to lang \return 1 if this is before other
96     bool operator <(DownloadDict other) const {
97         if(_from < other.fromLang()) return true;
98         if(_from > other.fromLang()) return false;
99         if(_to < other.toLang()) return true;
100         return false;
101     }
102
103 private:
104     QString _from, _to, _title, _link;
105     float _size;
106 };
107
108
109 #endif // DOWNLOADDICT_H