--- /dev/null
+/***************************************************************************
+* Copyright (C) 2003 by Hideki Ikemoto *
+* ikemo@users.sourceforge.jp *
+* *
+* This program is free software; you can redistribute it and/or modify *
+* it under the terms of the GNU General Public License as published by *
+* the Free Software Foundation; either version 2 of the License, or *
+* (at your option) any later version. *
+***************************************************************************/
+
+#include "kitawriteview.h"
+#include "libkita/qcp932codec.h"
+#include "libkita/kitaconfig.h"
+#include "kitahtmlpart.h"
+#include "kitahtmlview.h"
+#include "libkita/datmanager.h"
+#include "libkita/parsemisc.h"
+#include "libkita/signalcollection.h"
+#include "libkita/account.h"
+#include "libkita/kita-utf8.h"
+
+#include <ksqueezedtextlabel.h>
+#include <klocale.h>
+#include <kdebug.h>
+#include <kstandarddirs.h>
+#include <klineedit.h>
+#include <kmessagebox.h>
+#include <kio/job.h>
+
+#include <qapplication.h>
+#include <qtextedit.h>
+#include <qpushbutton.h>
+#include <qlabel.h>
+#include <qcombobox.h>
+#include <qeucjpcodec.h>
+#include <qfile.h>
+#include <qmessagebox.h>
+#include <qtabwidget.h>
+#include <qcheckbox.h>
+#include <qlayout.h>
+
+/* result code */
+enum{
+ K2ch_Unknown,
+ K2ch_True,
+ K2ch_False,
+ K2ch_Error,
+ K2ch_Check,
+ K2ch_Cookie,
+ K2ch_NewThread
+};
+
+
+/*--------------------------------------------------------------------*/
+
+QCp932Codec* KitaWriteView::m_cp932Codec = NULL;
+
+
+/*
+
+If mode == WRITEMODE_NORMAL, then write response.
+If mode == WRITEMODE_NEWTHREAD, then create the new thread. "newThreadName" is the name of the new thread.
+
+Call setMessage() to set message later.
+See also KitaWriteTabWidget::slotShowWriteView().
+
+Call slotPostMessage() to post the message.
+
+*/
+KitaWriteView::KitaWriteView( QWidget* parent, const char* name, const KURL& url, int mode, const QString& newThreadName )
+ : KitaWriteDialogBase( parent, name, 0 )
+{
+ m_mode = mode;
+
+ if( m_mode == WRITEMODE_NORMAL ) m_datURL = Kita::ParseMisc::parseURLonly( url );
+ else{
+ /* create new thread */
+
+ /* set dummy URL */
+ QRegExp threadcheck( "\\d+.dat" );
+ if( url.filename().find( threadcheck ) != -1 ) m_datURL = Kita::DatManager::boardURL( url );
+ else m_datURL = url;
+ m_datURL.addPath( "dat/dummy.dat" );
+ }
+ qDebug("url: %s\n",m_datURL.prettyURL().ascii());
+
+ m_bbscgi = Kita::DatManager::boardRoot( m_datURL ) + "test/bbs.cgi";
+
+ /* connect signals */
+ connect( buttonOk, SIGNAL( clicked() ),
+ SLOT( slotPostMessage() ) );
+
+ connect( buttonCancel, SIGNAL( clicked() ),
+ SLOT( slotCancel() ) );
+
+ connect( this, SIGNAL( openURLRequestExt(
+ const KURL&, const KParts::URLArgs&, QString, int, int,
+ const KURL&, const KURL&, const QString&, const QString& ) ),
+ Kita::SignalCollection::getInstance(), SIGNAL( openURLRequestExt(
+ const KURL& , const KParts::URLArgs&, QString, int, int,
+ const KURL&, const KURL&, const QString&, const QString& ) ) );
+
+ connect( faceCombo, SIGNAL( activated( const QString& ) ),
+ bodyText, SLOT( insert( const QString& ) ) );
+
+ connect( qtw, SIGNAL( currentChanged ( QWidget * ) ),
+ this, SLOT( slotCurrentChanged ( QWidget * ) ) );
+
+
+ /* setup preview view */
+ m_preview = new KitaHTMLPart( NULL );
+ qtw->addTab( m_preview->view(), i18n( "preview" ) );
+ m_preview->setup( HTMLPART_MODE_KHTML, m_datURL );
+ qtw->setCurrentPage( 0 );
+
+ /* setup labels and edit lines */
+ QFont font = KitaConfig::threadFont();
+ bodyText->setFont( font );
+ bodyText->setTabChangesFocus( TRUE );
+
+ boardNameLabel->setText( Kita::DatManager::boardName( m_datURL ) );
+ if( m_mode == WRITEMODE_NORMAL ){
+ threadName->setText( Kita::DatManager::threadName( m_datURL ) );
+ threadName->setReadOnly( TRUE );
+ threadName->setFrame( FALSE );
+ threadName->setFocusPolicy( NoFocus );
+ threadURLLabel->setText( Kita::DatManager::threadURL( m_datURL ) );
+ }
+ else{
+ threadName->setText( newThreadName );
+ threadURLLabel->setText( QString::null );
+ }
+
+ nameLine->setText( QString::null );
+ KCompletion* comp = KitaConfig::nameCompletion();
+ nameLine->setCompletionObject( comp );
+
+ mailLine->setText( "sage" );
+ sageBox->setChecked( true );
+ m_mailswap = "";
+
+ /* setup AA */
+ faceCombo->clear();
+ faceCombo->insertItem( "" );
+ QStringList list = KitaConfig::asciiArtList();
+ QStringList::iterator it;
+ for ( it = list.begin(); it != list.end(); ++it )
+ {
+ faceCombo->insertItem( *it );
+ }
+}
+
+
+KitaWriteView::~KitaWriteView()
+{
+ if ( m_preview ) {
+ delete m_preview;
+ }
+}
+
+
+/* public */
+void KitaWriteView::setMessage( const QString& bodyStr )
+{
+ bodyText->clear();
+ bodyText->insert( bodyStr );
+ bodyText->setFocus();
+}
+
+
+/* public */
+void KitaWriteView::insertMessage( const QString& str )
+{
+ bodyText->insert( str );
+ bodyText->setFocus();
+}
+
+
+/* public information */
+
+const QString KitaWriteView::name() const
+{
+ return nameLine->text();
+}
+
+const QString KitaWriteView::mail() const
+{
+ return mailLine->text();
+}
+
+const QString KitaWriteView::body() const
+{
+ return bodyText->text();
+}
+
+const KURL KitaWriteView::datURL() const
+{
+ return m_datURL;
+}
+
+
+/* public slot */ /* virtual */
+void KitaWriteView::setFocus()
+{
+ bodyText->setFocus();
+}
+
+
+/* call this slot to post the message. */ /* public slot */
+void KitaWriteView::slotPostMessage()
+{
+ if( bodyText->text().length() == 0 ) return;
+ if( m_mode == WRITEMODE_NEWTHREAD && threadName->text().length() == 0 ){
+ QMessageBox::warning( this, "Kita",i18n( "Enter the thread name." ) );
+ return;
+ }
+
+ /* fusianasan */
+ if( nameLine->text().length() == 0
+ && Kita::DatManager::getBBSDefaultName( m_datURL ) == "fusianasan" ){
+ QMessageBox::warning( this, "Kita",i18n( "fusianasan." ) );
+ return;
+ }
+
+ QString name = nameLine->text();
+ KitaConfig::addNameCompletionItem( name );
+
+ QString postStr = setPostStr();
+ QString refStr = Kita::DatManager::boardURL( m_datURL );
+
+ qDebug("daturl: %s\n",m_datURL.prettyURL().ascii());
+ qDebug("bbscgi: %s\n",m_bbscgi.prettyURL().ascii());
+ qDebug("%s\n",(const char *)postStr.local8Bit());
+ qDebug("referrer: %s\n",(const char *)refStr.local8Bit());
+
+ kdDebug() << "query: " << postStr << endl;
+ m_array.resize( 0 );
+
+ KIO::TransferJob* job = KIO::http_post( m_bbscgi, postStr.utf8(), true );
+ job->addMetaData( "content-type", "Content-type: application/x-www-form-urlencoded" );
+ job->addMetaData( "referrer", refStr );
+
+ connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ),
+ this, SLOT( slotRecieveData( KIO::Job*, const QByteArray& ) ) );
+
+ /* slotPostFinished() is called when done. */
+ connect( job, SIGNAL( result( KIO::Job* ) ),
+ this, SLOT( slotPostFinished( KIO::Job* ) ) );
+}
+
+
+/* public slot */
+void KitaWriteView::slotCancel()
+{
+ if( bodyText->text().length() == 0 ){
+ emit closeCurrentTab(); /* to KitaWriteTabWidget */
+ return;
+ }
+
+ switch ( QMessageBox::warning( this, "Kita",
+ i18n( "If you close this dialog, you will lose text.\n"
+ "Do you want to close?" ),
+ QMessageBox::Ok, QMessageBox::Cancel | QMessageBox::Default ) )
+ {
+ case QMessageBox::Ok:
+ setMessage( QString::null );
+ emit closeCurrentTab(); /* to KitaWriteTabWidget */
+ break;
+ case QMessageBox::Cancel:
+ // do nothing
+ break;
+ }
+}
+
+
+/* public slot */
+void KitaWriteView::slotEnableWriting( bool enable )
+{
+ buttonOk->setEnabled( enable );
+}
+
+
+/* see also slotPostMessage() */ /* private slot */
+void KitaWriteView::slotRecieveData( KIO::Job*, const QByteArray& data )
+{
+ m_array.append( data.data() );
+}
+
+
+/* This slot is called when posting is done. */
+/* see also slotPostMessage() */ /* private slot */
+void KitaWriteView::slotPostFinished( KIO::Job* )
+{
+ QString response = QTextCodec::codecForName( "sjis" ) ->toUnicode( m_array );
+ int retcode = resultCode( response );
+
+ qDebug("\ncode: %d\n",retcode );
+ qDebug("%s\n", (const char *)response.local8Bit());
+
+ switch ( retcode )
+ {
+
+ case K2ch_True:
+
+ /* save log */
+ logPostMessage();
+
+ /* clear message */
+ setMessage( QString::null );
+
+ /* reload thread */
+ if( m_mode == WRITEMODE_NORMAL ){
+
+ emit openURLRequestExt( m_datURL, KParts::URLArgs(), "kita_open_2chthread", 0 );
+
+ emit closeCurrentTab(); /* to KitaWriteTabWidget */
+ }
+
+ /* reload board */
+ else{
+
+ QString board = Kita::DatManager::boardURL( m_datURL );
+ emit openURLRequestExt( board, KParts::URLArgs(), "kita_open_2chboard", 0 );
+
+ emit closeCurrentTab(); /* to KitaWriteTabWidget */
+ }
+
+ break;
+
+ case K2ch_Unknown:
+ case K2ch_False:
+ case K2ch_Check:
+ case K2ch_Error:
+
+ KMessageBox::error( 0, resultMessage( response ), resultTitle( response ) );
+
+ break;
+
+ /* eat cookie, then re-post message */
+ case K2ch_Cookie:
+
+ if ( KMessageBox::questionYesNo( 0,
+ QTextCodec::codecForName( "utf8" )->toUnicode( KITAUTF8_WRITECOOKIEMSG ),
+ resultTitle( response ) )
+ == KMessageBox::Yes )
+ {
+ slotPostMessage();
+ }
+
+ break;
+
+ /* re-post message to create the new thread */
+ case K2ch_NewThread:
+
+ if ( KMessageBox::questionYesNo( 0,
+ QTextCodec::codecForName( "utf8" )->toUnicode( KITAUTF8_WRITENEWTHREAD ),
+ resultTitle( response ) )
+ == KMessageBox::Yes )
+ {
+ m_bbscgi = Kita::DatManager::boardRoot( m_datURL ) + "test/subbbs.cgi";
+ slotPostMessage();
+ }
+
+ break;
+
+ default:
+ break;
+ }
+}
+
+
+/* update preview screen */ /* private slot */
+void KitaWriteView::slotCurrentChanged ( QWidget * w )
+{
+ if ( w == bodyText ){
+ w->setFocus();
+ return ;
+ }
+ if( m_preview == NULL ) return;
+
+ int resnum;
+
+ if( m_mode == WRITEMODE_NORMAL ) resnum = Kita::DatManager::getReadNum( m_datURL ) + 1 ;
+ else resnum = 1;
+
+ QDateTime now = QDateTime::currentDateTime();
+ QString bodystr = body();
+ bodystr.replace( "<", "<" ).replace( ">", ">" ).replace( "\n", " <br> " );
+ QString namestr = name();
+ if ( namestr == NULL || namestr.length() == 0 ) namestr = Kita::DatManager::getBBSDefaultName( m_datURL );
+ QString rawData = namestr + "<>" + mail() + "<>"
+ + now.toString( "yy/MM/dd hh:mm:ss" )
+ + "<> " + bodystr + " <>";
+
+ QString htmlstr = Kita::ParseMisc::DatToHtml( rawData, resnum, KitaConfig::showMailAddress() );
+ m_preview->setInnerHTML( htmlstr );
+}
+
+
+/* private */
+QString KitaWriteView::setPostStr()
+{
+ QString ret;
+ if( !m_cp932Codec ) m_cp932Codec = new QCp932Codec();
+
+ int mib = m_cp932Codec->mibEnum();
+ int serverTime;
+
+ if( m_mode == WRITEMODE_NORMAL ) serverTime = Kita::DatManager::getServerTime( m_datURL );
+ /* TODO: get time from server */
+ else serverTime = QDateTime::currentDateTime().toTime_t() - 10*60;
+
+ /* submit ( URL encoded ) */
+ if( m_mode == WRITEMODE_NORMAL ) ( ret += "submit=" ) += "%8f%91%82%ab%8d%9e%82%de"; /* kakikomu */
+ else ( ret += "submit=" ) += "%90V%8bK%83X%83%8c%83b%83h%8d%ec%90%ac"; /* sinki thread sakusei */
+
+ /* subject */
+ if( m_mode == WRITEMODE_NEWTHREAD ) ( ret += "&subject=" ) += KURL::encode_string( threadName->text(), mib );
+
+ /* from, mail, message, bbs */
+ ( ret += "&FROM=" ) += KURL::encode_string( nameLine->text(), mib );
+ ( ret += "&mail=" ) += KURL::encode_string( mailLine->text(), mib );
+ ( ret += "&MESSAGE=" ) += KURL::encode_string( bodyText->text(), mib );
+ ( ret += "&bbs=" ) += Kita::DatManager::boardID( m_datURL );
+
+ /* key */
+ if( m_mode == WRITEMODE_NORMAL ) ( ret += "&key=" ) += Kita::DatManager::threadID( m_datURL );
+
+ /* time */
+ ( ret += "&time=" ) += QString::number( serverTime );
+
+ /* login */
+ if ( Kita::DatManager::is2chThread( m_datURL ) && Kita::Account::isLogged() ) {
+ ret += "&sid=";
+ ret += KURL::encode_string( Kita::Account::getSessionID() );
+ }
+
+ return ret;
+}
+
+
+/* save post log */ /* private */
+void KitaWriteView::logPostMessage()
+{
+ QString boardName = Kita::DatManager::boardName( m_datURL );
+ QString threadName = Kita::DatManager::threadName( m_datURL );
+ QString threadURL = Kita::DatManager::threadURL( m_datURL );
+
+ QDateTime now = QDateTime::currentDateTime();
+ QString logPath = locateLocal( "appdata", "log.txt" );
+
+ QFile file( logPath );
+ if ( file.open( IO_WriteOnly | IO_Append ) )
+ {
+ QTextStream stream( &file );
+ stream.setEncoding( QTextStream::UnicodeUTF8 );
+
+ stream << "Date : " << now.toString( "yyyy/MM/dd hh:mm:ss" ) << endl; // current time
+ stream << "Board : " << boardName << endl;
+ stream << "Thread : " << threadName << endl;
+ stream << "URL : " << threadURL << endl;
+ stream << "Name : " << nameLine->text() << endl;
+ stream << "Mail : " << mailLine->text() << endl << endl;
+ stream << bodyText->text() << endl;
+ stream << "----------------------------------------" << endl;
+
+ file.close();
+ }
+}
+
+
+/* get result code from 2ch tag or title. */ /* private */
+int KitaWriteView::resultCode( const QString& response ) const
+{
+ int retcode = K2ch_Unknown;;
+
+ QRegExp regexp( "<!-- 2ch_X:(.*) -->" );
+ int pos = regexp.search( response );
+
+ QString k2ch_X;
+ if ( pos != -1 ) { /* get code from 2ch tag */
+
+ k2ch_X = regexp.cap( 1 );
+
+ if ( k2ch_X == "true" ) retcode = K2ch_True;
+ else if ( k2ch_X == "false" ) retcode = K2ch_False;
+ else if ( k2ch_X == "error" ) retcode = K2ch_Error;
+ else if ( k2ch_X == "check" ) retcode = K2ch_Check;
+ else if ( k2ch_X == "cookie" ) retcode = K2ch_Cookie;
+
+ } else { /* get code from title */
+
+ QString title = resultTitle( response );
+
+ if( title != QString::null ){
+
+ /* see also libkita/kita-utf8.h */
+ QString errstr = QTextCodec::codecForName( "utf8" )->toUnicode( KITAUTF8_WRITEERROR );
+ QString truestr = QTextCodec::codecForName( "utf8" )->toUnicode( KITAUTF8_WRITETRUE );
+ QString ckstr = QTextCodec::codecForName( "utf8" )->toUnicode( KITAUTF8_WRITECOOKIE );
+
+ if( title.contains( errstr ) ) retcode = K2ch_Error;
+ else if( title.contains( truestr ) ) retcode = K2ch_True;
+ else if( title.contains( ckstr ) ) retcode = K2ch_Cookie;
+ else if( m_mode == WRITEMODE_NEWTHREAD
+ && response.contains( ckstr )
+ && response.contains( "subbbs.cgi" ) ) retcode = K2ch_NewThread;
+
+ }
+ }
+
+ return retcode;
+}
+
+
+/* private */
+QString KitaWriteView::resultMessage( const QString& response ) const
+{
+ QRegExp regexp( "<b>([^<]*)</b>" );
+ int pos = regexp.search( response );
+ if ( pos != -1 ) {
+ return regexp.cap( 1 );
+ }
+
+ QRegExp body_regexp( "<body>([^<]*)</body>" );
+ if ( body_regexp.search( response ) != -1 ) {
+ return body_regexp.cap( 1 );
+ }
+
+ return QString::null;
+}
+
+
+/* private */
+QString KitaWriteView::resultTitle( const QString& response ) const
+{
+ QRegExp regexp( "<title>(.*)</title>" );
+ regexp.setCaseSensitive( FALSE );
+ int pos = regexp.search( response );
+ if ( pos != -1 ) {
+ return regexp.cap( 1 );
+ } else {
+ return QString::null;
+ }
+}