DlgTipOfTheDayImp.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00068
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
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
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
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"