AlbumShaper
1.0a3
|
00001 //============================================== 00002 // copyright : (C) 2003-2005 by Will Stokes 00003 //============================================== 00004 // This program is free software; you can redistribute it 00005 // and/or modify it under the terms of the GNU General 00006 // Public License as published by the Free Software 00007 // Foundation; either version 2 of the License, or 00008 // (at your option) any later version. 00009 //============================================== 00010 00011 //Systemwide includes 00012 #include <qlayout.h> 00013 #include <qlabel.h> 00014 #include <qfont.h> 00015 #include <qframe.h> 00016 #include <qprogressbar.h> 00017 #include <qfile.h> 00018 #include <qdom.h> 00019 #include <qstringlist.h> 00020 #include <qtooltip.h> 00021 #include <qpixmap.h> 00022 #include <qdir.h> 00023 #include <qmovie.h> 00024 #include <qtimer.h> 00025 #include <qsizegrip.h> 00026 00027 //Projectwide includes 00028 #include "clickableLabel.h" 00029 #include "statusWidget.h" 00030 #include "titleWidget.h" 00031 #include "window.h" 00032 #include "../config.h" 00033 #include "../configuration/configuration.h" 00034 00035 //============================================== 00036 StatusWidget::StatusWidget(QWidget *parent, 00037 const char* name ) : QWidget(parent,name) 00038 { 00039 //create status message 00040 message = new QLabel( this ); 00041 message->setText( "" ); 00042 00043 //create timer object and setup signals 00044 timer = new QTimer(); 00045 connect(timer, SIGNAL(timeout()), this, SLOT(removeStatus()) ); 00046 00047 //create progress message and bar 00048 progressBar = new QProgressBar( this ); 00049 progressBar->setCenterIndicator(true); 00050 progressBar->hide(); 00051 curStep = 0; 00052 00053 //----------------------------------------------------------------- 00054 //setup http object to check for updates, only check for updates if they are enabled 00055 updateAvailable = NULL; 00056 http.setHost( "albumshaper.sourceforge.net" ); 00057 connect( &http, SIGNAL(done(bool)), this, SLOT(fileFetched(bool)) ); 00058 if(((Window*)parentWidget())->getConfig()->getBool( "alerts", "showSoftwareUpdateAlerts")) 00059 { 00060 checkForUpdates(); 00061 } 00062 //----------------------------------------------------------------- 00063 //place progress frame and status message in main grid 00064 grid = new QGridLayout( this, 1, 6, 0 ); 00065 grid->setSpacing(WIDGET_SPACING); 00066 grid->setColSpacing( 0, WIDGET_SPACING ); 00067 grid->addWidget( message, 0, 1, Qt::AlignVCenter ); 00068 grid->addWidget( progressBar, 0, 2, Qt::AlignVCenter ); 00069 grid->setColStretch( 3, 1 ); 00070 00071 //PLATFORM_SPECIFIC_CODE 00072 //mac os x puts in a size grip that can interfere with the updates icon, in order 00073 //to avoid this we manually place the size grip ourselves 00074 //windows users expect a grip too, but qt doesn't put one in by default. we'll add 00075 //it for them too. :-) 00076 #if defined(Q_OS_MACX) || defined(Q_OS_WIN) 00077 QSizeGrip* sizeGrip = new QSizeGrip( this ); 00078 grid->addWidget( sizeGrip, 0, 5, Qt::AlignBottom ); 00079 #endif 00080 00081 } 00082 //============================================== 00083 StatusWidget::~StatusWidget() 00084 { 00085 delete timer; 00086 timer = NULL; 00087 } 00088 //============================================== 00089 void StatusWidget::showProgressBar(QString message, int numSteps) 00090 { 00091 //make sure timer is stopped so progress mess is never hidden 00092 //this can occur if a new event is begun before the previous events message is removed after default delay 00093 timer->stop(); 00094 00095 //setup progress bar and show it 00096 this->message->setText( message ); 00097 progressBar->setProgress( 0, numSteps ); 00098 progressBar->show(); 00099 curStep = 0; 00100 } 00101 //============================================== 00102 void StatusWidget::updateProgress(int progress, QString newMessage) 00103 { 00104 curStep = progress; 00105 progressBar->setProgress( progress ); 00106 00107 //update message if provided 00108 if(newMessage != QString::null) 00109 { 00110 this->message->setText( newMessage ); 00111 } 00112 } 00113 //============================================== 00114 int StatusWidget::currentProgress() 00115 { 00116 return curStep; 00117 } 00118 //============================================== 00119 void StatusWidget::incrementProgress() 00120 { 00121 curStep++; 00122 progressBar->setProgress( curStep ); 00123 } 00124 //============================================== 00125 void StatusWidget::setStatus( QString message ) 00126 { 00127 timer->stop(); 00128 00129 //hide progress bar 00130 progressBar->hide(); 00131 00132 //update status message 00133 this->message->setText( message ); 00134 00135 timer->start( 2000, TRUE ); 00136 } 00137 //============================================== 00138 void StatusWidget::removeStatus() 00139 { 00140 //set status message to empty string 00141 message->setText( "" ); 00142 } 00143 //============================================== 00144 void StatusWidget::fileFetched(bool error) 00145 { 00146 //------------------------------------------------------------ 00147 //if unable to get file bail 00148 if(error) 00149 { 00150 return; 00151 } 00152 //------------------------------------------------------------ 00153 //write releases to temp file 00154 QFile fetchedDoc( TEMP_DIR + QString("/releases.xml") ); 00155 if(fetchedDoc.open(IO_WriteOnly)) 00156 { 00157 //---------------------------- 00158 //write to file 00159 QTextStream stream( &fetchedDoc ); 00160 stream.setEncoding( QTextStream::UnicodeUTF8 ); 00161 stream << QString( http.readAll() ); 00162 fetchedDoc.close(); 00163 //---------------------------- 00164 //parse xml file, construct string list of releases 00165 //open file, bail if unable to 00166 if( !fetchedDoc.open( IO_ReadOnly ) ) 00167 { 00168 return; 00169 } 00170 00171 //parse dom 00172 QDomDocument xmlDom; 00173 if( !xmlDom.setContent( &fetchedDoc ) ) 00174 { 00175 fetchedDoc.close(); 00176 return; 00177 } 00178 00179 //close file 00180 fetchedDoc.close(); 00181 00182 //construct stringlist of releases 00183 //actually, only get the first release since we don't need the others to determine if we 00184 //are out of date 00185 00186 QStringList releases; 00187 QDomElement root = xmlDom.documentElement(); 00188 QDomNode node = root.firstChild(); 00189 QDomText val; 00190 bool thisVersionFound = false; 00191 while( !node.isNull() ) 00192 { 00193 if( node.isElement() && node.nodeName() == "release" ) 00194 { 00195 val = node.firstChild().toText(); 00196 if(!val.isNull()) 00197 { 00198 //append release # 00199 releases.append( QString(val.nodeValue()) ); 00200 00201 //is release this version? 00202 if( QString(val.nodeValue()).compare( QString(ALBUMSHAPER_VERSION) ) == 0 ) 00203 thisVersionFound = true; 00204 } 00205 } 00206 node = node.nextSibling(); 00207 } 00208 00209 //compare first release to this release, if strings not equal then we're outdated, 00210 //update album shaper icon and start grabbing changelogs 00211 if(thisVersionFound && releases.first().compare( QString(ALBUMSHAPER_VERSION) ) != 0) 00212 { 00213 ClickableLabel* uA = new ClickableLabel( this ); 00214 uA->setMovie( QMovie( QString(IMAGE_PATH)+"miscImages/updateAvailable.mng") ); 00215 QToolTip::add( uA, tr("Your copy of Album Shaper is not up to date! Click here for details") ); 00216 grid->addWidget( uA, 0, 4, Qt::AlignVCenter ); 00217 connect( uA, SIGNAL(clicked()), 00218 ((Window*)parentWidget())->getTitle(), SLOT(aboutProgram()) ); 00219 uA->show();\ 00220 updateAvailable = uA; 00221 } 00222 } 00223 //------------------------------------------------------------ 00224 } 00225 //============================================== 00226 void StatusWidget::checkForUpdates() 00227 { 00228 if(updateAvailable != NULL) 00229 return; 00230 00231 //attempt to get releases list from website. this lets us find out if this 00232 //copy of Album Shaper is outdated 00233 http.get( "/webService/releases.xml"); 00234 } 00235 //============================================== 00236 void StatusWidget::removeUpdatesIcon() 00237 { 00238 delete updateAvailable; 00239 updateAvailable = NULL; 00240 } 00241 //============================================== 00242 void StatusWidget::grabInput() 00243 { 00244 grabKeyboard(); 00245 grabMouse(); 00246 } 00247 //============================================== 00248 void StatusWidget::releaseInput() 00249 { 00250 releaseKeyboard(); 00251 releaseMouse(); 00252 } 00253 //==============================================