DlgTipOfTheDayImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <qcheckbox.h>
00028 # include <qfile.h>
00029 # include <qlabel.h>
00030 # include <qlayout.h>
00031 # include <qtextstream.h>
00032 # include <QHttp>
00033 #endif
00034 
00035 #include "DlgTipOfTheDayImp.h"
00036 #include "Application.h"
00037 #include "MainWindow.h"
00038 
00039 #include <Base/Parameter.h>
00040 #include <Base/Console.h>
00041 
00042 using namespace Gui::Dialog;
00043 
00044 /* TRANSLATOR Gui::Dialog::DlgTipOfTheDayImp */
00045 
00053 DlgTipOfTheDayImp::DlgTipOfTheDayImp( QWidget* parent, Qt::WFlags fl )
00054   : QDialog( parent, fl | Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
00055   WindowParameter("General")
00056 {
00057     setupUi(this);
00058     _http = new QHttp;
00059     connect(_http, SIGNAL(done(bool)), this, SLOT(onDone(bool)));
00060     connect(_http, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
00061     connect(_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), 
00062             this, SLOT(onResponseHeaderReceived(const QHttpResponseHeader &)));
00063 
00064     bool tips = getWindowParameter()->GetBool("Tipoftheday", true);
00065     checkShowTips->setChecked(tips);
00066 
00067     // Since the resize mode of DlgTipOfTheDayBase does not
00068     // work properly so set this by hand 
00069     setMinimumSize(QSize(320, 250));
00070     layout()->setSizeConstraint( QLayout::SetNoConstraint );
00071 
00072     reload();
00073     on_buttonNextTip_clicked();
00074 }
00075 
00077 DlgTipOfTheDayImp::~DlgTipOfTheDayImp()
00078 {
00079     delete _http;
00080     getWindowParameter()->SetBool("Tipoftheday", checkShowTips->isChecked());
00081 }
00082 
00084 void DlgTipOfTheDayImp::on_buttonNextTip_clicked()
00085 {
00086     _iCurrentTip = (_iCurrentTip + 1) % _lTips.size();
00087     textTip->setText(_lTips[_iCurrentTip]);
00088 }
00089 
00091 void DlgTipOfTheDayImp::reload()
00092 {
00093     // set the host and start the download
00094     _http->setHost(QLatin1String("apps.sourceforge.net"));
00095     _http->get(QLatin1String("/mediawiki/free-cad/index.php?title=Tip_of_the_day"), 0);
00096 
00097     _iCurrentTip = 0;
00098     _lTips << tr("If you want to learn more about FreeCAD you must go to %1 or press the Help item in the Help menu.")
00099         .arg(QLatin1String("<a href=\"http://apps.sourceforge.net/mediawiki/free-cad/\">"
00100                            "http://apps.sourceforge.net/mediawiki/free-cad/</a>"));
00101 }
00102 
00103 void DlgTipOfTheDayImp::onResponseHeaderReceived(const QHttpResponseHeader & responseHeader)
00104 {
00105     if (responseHeader.statusCode() != 200) {
00106         QString msg = tr("Download failed: %1\n").arg(responseHeader.reasonPhrase());
00107         Base::Console().Log(msg.toAscii());
00108         _http->abort();
00109     }
00110 }
00111 
00112 void DlgTipOfTheDayImp::onDone(bool err)
00113 {
00114     if (err)
00115         return;
00116 
00117     // get the page and search for the tips section
00118     QString text = QString::fromAscii(_http->readAll());
00119     QRegExp rx(QLatin1String("<p>You find the latest information.+<div class=\"printfooter\">"));
00120     if (rx.indexIn(text) > -1) {
00121         // the text of interest
00122         text = rx.cap();
00123         rx.setPattern(QLatin1String("<ul><li>.+</li></ul>\n"));
00124         rx.setMinimal(true);
00125         _lTips += text.split(rx, QString::SkipEmptyParts);
00126     }
00127 }
00128 
00132 void DlgTipOfTheDayImp::onStateChanged (int state)
00133 {
00134     switch (state) {
00135         case QHttp::Connecting:
00136             Base::Console().Log("Connecting to host...\n");
00137             break;
00138         case QHttp::Sending:
00139             Base::Console().Log("Sending to host...\n");
00140             break;
00141         case QHttp::Reading:
00142             Base::Console().Log("Reading from host...\n");
00143             break;
00144         case QHttp::Closing:
00145         case QHttp::Unconnected:
00146             Base::Console().Log("%s\n",(const char*)_http->errorString().toAscii());
00147             break;
00148         default:
00149             break;
00150   }
00151 }
00152 
00153 #include "moc_DlgTipOfTheDayImp.cpp"

Generated on Wed Nov 23 19:00:07 2011 for FreeCAD by  doxygen 1.6.1