OSDN Git Service

Move the directories
[kita/kita.git] / src / libkita / account.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
5 *   This program 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 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10 #include "account.h"
11
12 #include <QtCore/QByteArray>
13 #include <QtCore/QRegExp>
14
15 #include <kurl.h>
16 #include <kio/job.h>
17 #include <kio/jobuidelegate.h>
18 #include <kio/slaveconfig.h>
19
20 #include "config.h"
21
22 using namespace Kita;
23
24 Account* Account::instance = 0;
25
26 Account* Account::getInstance()
27 {
28     if (instance == 0) {
29         instance = new Account();
30     }
31     return instance;
32 }
33
34 Account::Account()
35         : m_isLogged(false)
36 {}
37
38 Account::~Account()
39 {}
40
41 bool Account::login(const QString& userID, const QString& password)
42 {
43     return getInstance() ->loginInternal(userID, password);
44 }
45
46 bool Account::loginInternal(const QString& userID, const QString& password)
47 {
48     m_data.clear();
49
50     KUrl url("https://2chv.tora3.net/futen.cgi");
51     QString postData = QString("ID=") + userID + QString("&PW=") + password;
52
53     KIO::SlaveConfig::self() ->setConfigData("https",
54             url.host(),
55             "UserAgent",
56             "DOLIB/1.00");
57     m_job = KIO::http_post(url, postData.toUtf8(), false);
58
59     connect(m_job, SIGNAL(data(KIO::Job*, const QByteArray&)),
60              SLOT(slotReceiveData(KIO::Job*, const QByteArray&)));
61     connect(m_job, SIGNAL(result(KIO::Job*)), SLOT(slotResult(KIO::Job*)));
62     m_job->addMetaData("customHTTPHeader",
63                         QString("X-2ch-UA: Kita/%1").arg(VERSION));
64     m_job->addMetaData("content-type",
65                         "Content-Type: application/x-www-form-urlencoded");
66
67     QEventLoop m_eventLoop;
68     m_eventLoop.exec();
69
70     return m_isLogged;
71 }
72
73 void Account::slotReceiveData(KIO::Job*, const QByteArray& data)
74 {
75     m_data += data;
76 }
77
78 void Account::slotResult(KIO::Job* job)
79 {
80     m_job = 0;
81     if (job->error()) {
82         job->ui()->setWindow(0);
83         job->ui()->showErrorMessage();
84     }
85
86     QString str(m_data);
87     QRegExp regexp("SESSION-ID=(.*)");
88     if (regexp.indexIn(str) == -1) {
89         m_sessionID.clear();
90         m_isLogged = false;
91     } else {
92         QString value = regexp.cap(1);
93
94         QRegExp error("^ERROR:p+$");
95         if (error.indexIn(value) == -1) {
96             m_isLogged = true;
97             m_sessionID = value;
98         } else {
99             m_isLogged = false;
100             m_sessionID.clear();
101         }
102     }
103     m_eventLoop.exit();
104 }