ip2cparser.h
1 //------------------------------------------------------------------------------
2 // ip2cparser.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #ifndef __IP2CPARSER_H__
24 #define __IP2CPARSER_H__
25 
26 #include "ip2ccountry.h"
27 #include "ip2crange.h"
28 #include <QMutex>
29 #include <QThread>
30 
56 class IP2CParser : public QObject
57 {
58  Q_OBJECT
59 
60 public:
61  IP2CParser();
62 
67  bool isParsing() const
68  {
69  return bIsParsing;
70  }
71 
72  const QList<IP2CRange> ranges() const
73  {
74  return ranges_;
75  }
76 
77  bool readDatabase(const QString &filePath);
78  void readDatabaseThreaded(const QString &filePath);
79 
80 signals:
87  void parsingFinished(bool bSuccess);
88 
89 private:
95  class ConstructorDestructorParserStateSetter
96  {
97  public:
98  ConstructorDestructorParserStateSetter(IP2CParser *pParser);
99  ~ConstructorDestructorParserStateSetter();
100 
101  private:
102  IP2CParser *pParser;
103  };
104 
105  class ParsingThread : public QThread
106  {
107  public:
108  bool bSuccessState;
109  QString filePath;
110  IP2CParser *pParser;
111 
112 
113  ParsingThread(IP2CParser *pParser, const QString &filePath)
114  {
115  bSuccessState = false;
116  this->filePath = filePath;
117  this->pParser = pParser;
118  }
119 
120  void run() override;
121  };
122 
123  bool bIsParsing;
124  ParsingThread *currentParsingThread;
125  QList<IP2CRange> ranges_;
126 
127  QMutex thisLock;
128 
129  bool doReadDatabase(const QString &filePath);
130  bool readDatabaseVersion2(const QByteArray &dataArray);
131 
132 private slots:
133  void parsingThreadFinished();
134 };
135 
136 #endif