OSDN Git Service

3239273897a13b0798d21ea11abf2798b244ea7b
[kita/kita.git] / kita / 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 <config.h>
13
14 #include <kurl.h>
15
16 #include <kio/slaveconfig.h>
17 #include <kio/netaccess.h>
18 #include <kio/jobclasses.h>
19 #include <kio/job.h>
20
21 #include <qregexp.h>
22 #include <qapplication.h>
23
24 using namespace Kita;
25
26 Account* Account::instance = 0;
27
28 Account* Account::getInstance()
29 {
30     if ( instance == 0 ) {
31         instance = new Account();
32     }
33     return instance;
34 }
35
36 Account::Account()
37         : m_isLogged( false )
38 {}
39
40 Account::~Account()
41 {}
42
43 bool Account::login( const QString& userID, const QString& password )
44 {
45     return getInstance()->loginInternal( userID, password );
46 }
47
48 bool Account::loginInternal( const QString& userID, const QString& password )
49 {
50     m_data.resize( 0 );
51
52     KURL url( "https://2chv.tora3.net/futen.cgi" );
53     QString postData = QString("ID=") + userID + QString("&PW=") + password;
54
55     KIO::SlaveConfig::self() ->setConfigData( "https",
56             url.host(),
57             "UserAgent",
58             "DOLIB/1.00" );
59     m_job = KIO::http_post( url, postData.utf8(), false );
60
61     connect( m_job, SIGNAL( data( KIO::Job*, const QByteArray& ) ),
62              SLOT( slotReceiveData( KIO::Job*, const QByteArray& ) ) );
63     connect( m_job, SIGNAL( result( KIO::Job* ) ), SLOT( slotResult( KIO::Job* ) ) );
64     m_job->addMetaData( "customHTTPHeader",
65                         QString( "X-2ch-UA: Kita/%1" ).arg( VERSION ) );
66     m_job->addMetaData( "content-type",
67                         "Content-Type: application/x-www-form-urlencoded" );
68
69     enter_loop();
70
71     return m_isLogged;
72 }
73
74 void Account::slotReceiveData( KIO::Job*, const QByteArray& data )
75 {
76     QCString str( data, data.size() );
77     m_data += str;
78 }
79
80 void Account::slotResult( KIO::Job* job )
81 {
82     m_job = 0;
83     if ( job->error() )
84     {
85         job->showErrorDialog();
86     }
87
88     QString str( m_data );
89     QRegExp regexp( "SESSION-ID=(.*)" );
90     if ( regexp.search( str ) == -1 )
91     {
92         m_sessionID = QString::null;
93         m_isLogged = false;
94     }
95     else
96     {
97         QString value = regexp.cap( 1 );
98
99         QRegExp error( "^ERROR:p+$" );
100         if ( error.search( value ) == -1 )
101         {
102             m_isLogged = true;
103             m_sessionID = value;
104         }
105         else
106         {
107             m_isLogged = false;
108             m_sessionID = QString::null;
109         }
110     }
111     qApp->exit_loop();
112 }
113
114 // from netaccess.cpp
115 void qt_enter_modal( QWidget* widget );
116 void qt_leave_modal( QWidget* widget );
117
118 void Account::enter_loop()
119 {
120     QWidget dummy( 0, 0, WType_Dialog | WShowModal );
121     dummy.setFocusPolicy( QWidget::NoFocus );
122     qt_enter_modal( &dummy );
123     qApp->enter_loop();
124     qt_leave_modal( &dummy );
125 }
126
127 #include "account.moc"