2012-08-30 00:12:39 +02:00
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
2012-08-28 22:50:40 +02:00
#include "logfiledialog.h"
2012-11-05 14:15:55 +01:00
#ifdef GUI_800x480
#include "ui_logfiledialog_800x480.h"
#else
2012-08-28 22:50:40 +02:00
#include "ui_logfiledialog.h"
2012-11-05 14:15:55 +01:00
#endif
2012-09-17 23:44:33 +02:00
#include <QtCore>
#include "guilog.h"
#include "configfile.h"
#include "mymessagebox.h"
2012-12-22 19:57:45 +01:00
#include <game_defs.h>
2012-12-20 14:29:43 -08:00
#include <net/uploaderthread.h>
2012-08-28 22:50:40 +02:00
2012-09-17 23:44:33 +02:00
LogFileDialog :: LogFileDialog ( QWidget * parent , ConfigFile * c ) :
2012-12-21 01:01:01 +01:00
QDialog ( parent ), myConfig ( c ),
ui ( new Ui :: LogFileDialog )
2012-08-28 22:50:40 +02:00
{
2012-12-21 01:01:01 +01:00
ui -> setupUi ( this );
2012-09-17 23:44:33 +02:00
2012-12-21 01:01:01 +01:00
ui -> label_animation -> setMaximumWidth ( 0 );
ui -> horizontalLayout_animation -> setSpacing ( 0 );
2012-12-19 11:21:24 +01:00
2012-09-17 23:44:33 +02:00
connect ( ui -> pushButton_deleteLog , SIGNAL ( clicked ()), this , SLOT ( deleteLogFile ()));
connect ( ui -> pushButton_exportLogHtml , SIGNAL ( clicked ()), this , SLOT ( exportLogToHtml ()));
connect ( ui -> pushButton_exportLogTxt , SIGNAL ( clicked ()), this , SLOT ( exportLogToTxt ()));
connect ( ui -> pushButton_saveLogAs , SIGNAL ( clicked ()), this , SLOT ( saveLogFileAs ()));
2012-12-21 01:01:01 +01:00
connect ( ui -> treeWidget_logFiles , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * )), this , SLOT ( showLogFilePreviewInit ()));
connect ( ui -> pushButton_analyseLogfile , SIGNAL ( clicked ()), this , SLOT ( uploadFile ()));
connect ( ui -> comboBox , SIGNAL ( currentIndexChanged ( int )), this , SLOT ( showLogFilePreviewSelected ()));
2012-12-20 14:29:43 -08:00
connect ( this , SIGNAL ( signalUploadCompleted ( QString , QString )), this , SLOT ( showLogAnalysis ( QString , QString )));
connect ( this , SIGNAL ( signalUploadError ( QString , QString )), this , SLOT ( showUploadError ( QString , QString )));
2012-11-30 01:15:21 +01:00
2012-12-20 14:29:43 -08:00
uploader . reset ( new UploaderThread ( this ));
2012-08-28 22:50:40 +02:00
}
LogFileDialog ::~ LogFileDialog ()
{
delete ui ;
}
2012-09-17 23:44:33 +02:00
void LogFileDialog :: exec ()
{
2012-12-20 14:29:43 -08:00
uploader -> Run ();
2012-09-17 23:44:33 +02:00
refreshLogFileList ();
QDialog :: exec ();
2012-12-20 14:29:43 -08:00
uploader -> SignalTermination ();
uploader -> Join ( THREAD_WAIT_INFINITE );
}
void LogFileDialog :: UploadCompleted ( const std :: string & filename , const std :: string & returnMessage )
{
signalUploadCompleted ( QString :: fromStdString ( filename ), QString :: fromStdString ( returnMessage ));
}
void LogFileDialog :: UploadError ( const std :: string & filename , const std :: string & errorMessage )
{
signalUploadError ( QString :: fromStdString ( filename ), QString :: fromStdString ( errorMessage ));
2012-09-17 23:44:33 +02:00
}
void LogFileDialog :: refreshLogFileList ()
{
QDir logFileDir ;
logFileDir . setPath ( QString :: fromUtf8 ( myConfig -> readConfigString ( "LogDir" ). c_str ()));
QStringList filters ;
filters << "*.pdb" ;
QFileInfoList dbFilesList = logFileDir . entryInfoList ( filters , QDir :: Files , QDir :: Time );
QFileInfo currentSqliteLogFile ( QString :: fromStdString ( myGuiLog -> getMySqliteLogFileName ()));
2012-12-21 01:01:01 +01:00
ui -> treeWidget_logFiles -> blockSignals ( TRUE );
2012-09-17 23:44:33 +02:00
ui -> treeWidget_logFiles -> clear ();
int i ;
for ( i = 0 ; i < dbFilesList . size (); i ++ ) {
QTreeWidgetItem * item = new QTreeWidgetItem ;
2012-12-13 17:06:54 +01:00
#ifdef GUI_800x480
2012-12-21 01:01:01 +01:00
item -> setText ( 0 , dbFilesList . at ( i ). fileName (). remove ( "pokerth-log-" ));
2012-12-13 17:06:54 +01:00
#else
2012-12-21 01:01:01 +01:00
item -> setText ( 0 , dbFilesList . at ( i ). fileName ());
2012-12-13 17:06:54 +01:00
#endif
2012-09-17 23:44:33 +02:00
item -> setData ( 0 , Qt :: UserRole , dbFilesList . at ( i ). absoluteFilePath ());
if ( currentSqliteLogFile . fileName () == dbFilesList . at ( i ). fileName ()) {
item -> setData ( 0 , Qt :: BackgroundColorRole , QColor ( Qt :: red ));
item -> setData ( 0 , Qt :: TextColorRole , QColor ( Qt :: white ));
item -> setData ( 0 , Qt :: UserRole + 1 , "current" );
}
ui -> treeWidget_logFiles -> addTopLevelItem ( item );
}
ui -> treeWidget_logFiles -> sortItems ( 0 , Qt :: DescendingOrder );
2012-12-21 01:01:01 +01:00
ui -> treeWidget_logFiles -> blockSignals ( FALSE );
ui -> treeWidget_logFiles -> setCurrentItem ( ui -> treeWidget_logFiles -> topLevelItem ( 0 ));
2012-09-17 23:44:33 +02:00
}
void LogFileDialog :: deleteLogFile ()
{
QList < QTreeWidgetItem *> selectedItemsList = ui -> treeWidget_logFiles -> selectedItems ();
if ( ! selectedItemsList . isEmpty () && ! ( selectedItemsList . size () == 1 && selectedItemsList . front () -> data ( 0 , Qt :: UserRole + 1 ). toString () == "current" )) {
int ret = MyMessageBox :: warning ( this , tr ( "PokerTH - Delete log files" ),
2012-12-21 01:01:01 +01:00
tr ( "Do you really want to delete the selected log files?" ),
QMessageBox :: Yes | QMessageBox :: No );
2012-09-17 23:44:33 +02:00
if ( ret == QMessageBox :: Yes ) {
for ( int i = 0 ; i < selectedItemsList . size (); ++ i ) {
if ( selectedItemsList . at ( i ) -> data ( 0 , Qt :: UserRole + 1 ). toString () != "current" ) {
if ( ! QFile :: remove ( selectedItemsList . at ( i ) -> data ( 0 , Qt :: UserRole ). toString ())) {
2012-12-20 14:29:43 -08:00
MyMessageBox :: warning ( this , tr ( "Remove log file" ), tr ( "PokerTH cannot remove this log file, please verify that you have write access to this file!" ), QMessageBox :: Close );
2012-09-17 23:44:33 +02:00
}
}
}
refreshLogFileList ();
}
}
}
void LogFileDialog :: exportLogToHtml ()
{
QTreeWidgetItem * selectedItem = ui -> treeWidget_logFiles -> currentItem ();
if ( selectedItem ) {
2012-12-21 01:01:01 +01:00
QFileInfo fi ( selectedItem -> data ( 0 , Qt :: UserRole ). toString ());
2012-09-17 23:44:33 +02:00
QString fileName = QFileDialog :: getSaveFileName ( this , tr ( "Export PokerTH log file to HTML" ),
QDir :: homePath () + "/" + fi . baseName () + ".html" ,
tr ( "PokerTH HTML log (*.html)" ));
if ( ! fileName . isEmpty ()) {
myGuiLog -> exportLogPdbToHtml ( selectedItem -> data ( 0 , Qt :: UserRole ). toString (), fileName );
}
}
}
void LogFileDialog :: exportLogToTxt ()
{
QTreeWidgetItem * selectedItem = ui -> treeWidget_logFiles -> currentItem ();
if ( selectedItem ) {
2012-12-21 01:01:01 +01:00
QFileInfo fi ( selectedItem -> data ( 0 , Qt :: UserRole ). toString ());
2012-09-17 23:44:33 +02:00
QString fileName = QFileDialog :: getSaveFileName ( this , tr ( "Export PokerTH log file to plain text" ),
QDir :: homePath () + "/" + fi . baseName () + ".txt" ,
tr ( "PokerTH plain text log (*.txt)" ));
if ( ! fileName . isEmpty ()) {
myGuiLog -> exportLogPdbToTxt ( selectedItem -> data ( 0 , Qt :: UserRole ). toString (), fileName );
}
}
}
void LogFileDialog :: saveLogFileAs ()
{
QTreeWidgetItem * selectedItem = ui -> treeWidget_logFiles -> currentItem ();
if ( selectedItem ) {
2012-12-21 01:01:01 +01:00
QFileInfo fi ( selectedItem -> data ( 0 , Qt :: UserRole ). toString ());
2012-09-17 23:44:33 +02:00
QString fileName = QFileDialog :: getSaveFileName ( this , tr ( "Save PokerTH log file" ),
QDir :: homePath () + "/" + fi . baseName () + ".pdb" ,
tr ( "PokerTH SQL log (*.pdb)" ));
if ( ! fileName . isEmpty ()) {
QFile :: copy ( selectedItem -> data ( 0 , Qt :: UserRole ). toString (), fileName );
}
}
}
2012-11-30 01:15:21 +01:00
void LogFileDialog :: showLogFilePreview ( ShowLogMode mode )
2012-09-17 23:44:33 +02:00
{
QTreeWidgetItem * selectedItem = ui -> treeWidget_logFiles -> currentItem ();
2012-12-21 01:01:01 +01:00
if ( mode == INIT_VIEW ) {
ui -> comboBox -> blockSignals ( TRUE );
ui -> comboBox -> clear ();
QList < int > gameIds = myGuiLog -> getGameList ( selectedItem -> data ( 0 , Qt :: UserRole ). toString ());
QList < int >:: const_iterator it ;
for ( it = gameIds . constBegin (); it != gameIds . constEnd (); ++ it ) {
ui -> comboBox -> addItem ( QString :: number ( * it ));
}
ui -> comboBox -> blockSignals ( FALSE );
}
2012-11-30 01:15:21 +01:00
2012-09-17 23:44:33 +02:00
if ( selectedItem ) {
2012-12-21 01:01:01 +01:00
myGuiLog -> showLog ( selectedItem -> data ( 0 , Qt :: UserRole ). toString (), ui -> textBrowser_logPreview , ui -> comboBox -> itemText ( ui -> comboBox -> currentIndex ()). toInt ());
2012-09-17 23:44:33 +02:00
}
QTextCursor cursor ( ui -> textBrowser_logPreview -> textCursor ());
cursor . movePosition ( QTextCursor :: Start );
ui -> textBrowser_logPreview -> setTextCursor ( cursor );
}
void LogFileDialog :: keyPressEvent ( QKeyEvent * event )
{
if ( event -> key () == Qt :: Key_Delete ) { /*Delete*/
if ( ui -> treeWidget_logFiles -> hasFocus ()) {
2012-12-21 01:01:01 +01:00
ui -> pushButton_deleteLog -> click ();
2012-09-17 23:44:33 +02:00
}
2012-12-21 01:01:01 +01:00
}
2012-09-17 23:44:33 +02:00
}
2012-11-01 20:33:04 +01:00
void LogFileDialog :: uploadFile ()
{
2012-12-17 00:50:01 +01:00
QTreeWidgetItem * selectedItem = ui -> treeWidget_logFiles -> currentItem ();
2012-11-01 20:33:04 +01:00
2012-12-20 14:29:43 -08:00
if ( selectedItem ) {
file . setFileName ( selectedItem -> data ( 0 , Qt :: UserRole ). toString ());
2012-11-01 20:33:04 +01:00
2012-12-20 14:29:43 -08:00
uploadInProgressAnimationStart ();
uploader -> QueueUpload (
2012-12-22 19:57:45 +01:00
"http://pokerth.net/log_file_analysis/upload.php" ,
2012-12-20 14:29:43 -08:00
"" ,
"" ,
file . fileName (). toStdString (),
file . size (),
"pdb_file" );
}
2012-11-01 20:33:04 +01:00
}
2012-12-19 11:21:24 +01:00
void LogFileDialog :: uploadInProgressAnimationStart ()
{
2012-12-20 14:29:43 -08:00
//const QString buttonText(tr("Upload in progress"));
ui -> pushButton_analyseLogfile -> setDisabled ( TRUE );
2012-12-19 11:21:24 +01:00
2012-12-21 01:01:01 +01:00
QMovie * movie = new QMovie ( ":/gfx/loader.gif" );
ui -> label_animation -> setMovie ( movie );
ui -> label_animation -> setMaximumWidth ( 32 );
ui -> horizontalLayout_animation -> setSpacing ( - 1 );
ui -> label_animation -> setGeometry ( 0 , 0 , 32 , 32 );
movie -> start ();
2012-12-19 11:21:24 +01:00
}
void LogFileDialog :: uploadInProgressAnimationStop ()
{
2012-12-21 01:01:01 +01:00
ui -> pushButton_analyseLogfile -> setDisabled ( FALSE );
2012-12-19 11:21:24 +01:00
2012-12-21 01:01:01 +01:00
ui -> label_animation -> setMaximumWidth ( 0 );
ui -> horizontalLayout_animation -> setSpacing ( 0 );
ui -> label_animation -> setGeometry ( 0 , 0 , 0 , 0 );
2012-12-19 11:21:24 +01:00
}
2012-12-20 14:29:43 -08:00
void LogFileDialog :: showLogAnalysis ( QString /*filename*/ , QString returnMessage )
{
uploadInProgressAnimationStop ();
2012-12-23 00:33:04 +01:00
returnMessage = returnMessage . trimmed ();
QString retStr ( returnMessage . mid ( 0 , returnMessage . indexOf ( ' ' )));
2012-12-20 14:29:43 -08:00
2012-12-23 00:33:04 +01:00
if ( retStr == LOG_UPLOAD_OK_STR ) {
QString hash ( returnMessage . mid ( retStr . size ()). trimmed ());
qDebug () << hash << endl ;
QDesktopServices :: openUrl ( QUrl ( "http://logfile-analysis.pokerth.net/?ID=" + hash ));
2012-12-20 14:29:43 -08:00
} else {
qDebug () << returnMessage << endl ;
QString serverMsg ( tr ( "Processing of the log file on the web server failed. \n Please verify that you are uploading a valid PokerTH log file." ));
2012-12-22 20:56:00 +01:00
// if there is an error code, display a corresponding message.
2012-12-23 00:33:04 +01:00
if ( retStr == LOG_UPLOAD_ERROR_STR ) {
QString errorId ( returnMessage . mid ( retStr . size ()). trimmed ());
2012-12-22 19:57:45 +01:00
serverMsg += " \n " + tr ( "Failure reason: " );
2012-12-23 00:33:04 +01:00
switch ( errorId . toInt ())
2012-12-22 19:57:45 +01:00
{
case LOG_UPLOAD_ERROR_NO_FILE :
serverMsg += tr ( "No file received." );
break ;
case LOG_UPLOAD_ERROR_MAX_NUM_TOTAL :
serverMsg += tr ( "File rejected because of too many uploads." );
break ;
case LOG_UPLOAD_ERROR_MAX_NUM_IP :
serverMsg += tr ( "File rejected because of too many recent uploads. Please try again later." );
break ;
case LOG_UPLOAD_ERROR_FILE_SIZE :
serverMsg += tr ( "The file is too large." );
break ;
case LOG_UPLOAD_ERROR_FILE_EXT :
case LOG_UPLOAD_ERROR_FILE_HEAD :
serverMsg += tr ( "This file is not a valid and current PokerTH log file." );
break ;
case LOG_UPLOAD_ERROR_OPEN_DB :
case LOG_UPLOAD_ERROR_ID :
case LOG_UPLOAD_ERROR_FILE_MOVE :
case LOG_UPLOAD_ERROR_INSERT_DB :
default :
2012-12-22 20:56:00 +01:00
serverMsg += tr ( "Internal error. Please try again later. ID: " ) + returnMessage ;
2012-12-22 19:57:45 +01:00
break ;
}
2012-12-20 14:29:43 -08:00
}
MyMessageBox :: warning (
this , tr ( "Uploading log file" ), serverMsg , QMessageBox :: Close );
}
}
void LogFileDialog :: showUploadError ( QString /*filename*/ , QString errorMessage )
{
uploadInProgressAnimationStop ();
QString uploadMsg ( tr ( "Upload failed. Please check your internet connection! \n Uploading log files may fail if you are using an http proxy." ));
uploadMsg += " \n " + tr ( "Failure reason: " ) + errorMessage ;
MyMessageBox :: warning (
this , tr ( "Uploading log file" ), uploadMsg , QMessageBox :: Close );
}