newsdlg.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006-2007 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "newsdlg.h"
  17. #include <kcmultidialog.h>
  18. #include <kcmoduleinfo.h>
  19. #include <krun.h>
  20. #include <kurl.h>
  21. #include <kio/netaccess.h>
  22. #include <kmessagebox.h>
  23. #ifdef HAVE_KSHAREDCONFIG_H
  24. #include <ksharedconfig.h>
  25. #endif
  26. #include <QVariant>
  27. #include <QImage>
  28. #include <QPixmap>
  29. #include <QDateTime>
  30. #include <QString>
  31. #include <Q3ListBox>
  32. #include <QColor>
  33. #include <QPainter>
  34. #include <QPen>
  35. #include <cstdio>
  36. #include <cstring>
  37. #include "ktigcc.h"
  38. class ColoredListBoxText : public Q3ListBoxText {
  39. public:
  40. ColoredListBoxText(Q3ListBox *listbox, const QString &text,
  41. const QColor &textColor)
  42. : Q3ListBoxText(listbox,text), color(textColor) {}
  43. virtual ~ColoredListBoxText() {}
  44. protected:
  45. virtual void paint(QPainter *painter) {
  46. QPen oldPen=painter->pen();
  47. QPen pen=oldPen;
  48. pen.setColor(color);
  49. painter->setPen(pen);
  50. Q3ListBoxText::paint(painter);
  51. painter->setPen(oldPen);
  52. }
  53. private:
  54. QColor color;
  55. };
  56. bool NewsDialog::loadNews()
  57. {
  58. QString tmpFile;
  59. bool result=FALSE;
  60. pconfig->setGroup("News Headlines");
  61. QDateTime defaultDateTime;
  62. QDate latestHeadline=pconfig->readDateTimeEntry("Latest Headline",
  63. &defaultDateTime).date();
  64. if(KIO::NetAccess::download(
  65. KUrl("http://tigcc.ticalc.org/linux/newsheadlines.txt"),tmpFile,this)) {
  66. #define ERROR(s) do {KMessageBox::error(this,(s)); goto done;} while(0)
  67. #define ZAP_LF() do {char *p=line+(std::strlen(line)-1); if (*p=='\n') *p=0;} while(0)
  68. #define ZAP_CR() do {char *p=line+(std::strlen(line)-1); if (*p=='\r') *p=0;} while(0)
  69. std::FILE *f=std::fopen(tmpFile,"r");
  70. if (!f) ERROR("Downloading news failed.");
  71. char line[32768];
  72. // "TIGCC News Format"
  73. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  74. ZAP_LF();ZAP_CR();
  75. if (std::strcmp(line,"TIGCC News Format")) ERROR("Invalid news file.");
  76. // Empty line
  77. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  78. ZAP_LF();ZAP_CR();
  79. if (*line) ERROR("Invalid news file.");
  80. newsListBox->clear();
  81. while (1) {
  82. bool itemIsNew=FALSE;
  83. unsigned y,m,d;
  84. // Date
  85. if (!std::fgets(line,32768,f)) goto done;
  86. ZAP_LF();ZAP_CR();
  87. if (!*line) goto done;
  88. if (std::sscanf(line,"%4u%2u%2u",&y,&m,&d)<3) ERROR("Invalid news file.");
  89. if (latestHeadline.isNull() || QDate(y,m,d)>latestHeadline) {
  90. if (!result) {
  91. pconfig->writeEntry("Latest Headline",QDateTime(QDate(y,m,d)));
  92. pconfig->sync();
  93. }
  94. result=itemIsNew=TRUE;
  95. }
  96. // Title
  97. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  98. ZAP_LF();ZAP_CR();
  99. new ColoredListBoxText(newsListBox,QString::fromUtf8(line),
  100. itemIsNew?Qt::red:Qt::gray);
  101. // Empty line
  102. if (!std::fgets(line,32768,f)) goto done;
  103. ZAP_LF();ZAP_CR();
  104. if (*line) ERROR("Invalid news file.");
  105. }
  106. #undef ERROR
  107. #undef ZAP_LF
  108. #undef ZAP_CR
  109. done: if (f) std::fclose(f);
  110. KIO::NetAccess::removeTempFile(tmpFile);
  111. } else {
  112. KMessageBox::error(this,KIO::NetAccess::lastErrorString());
  113. }
  114. return result;
  115. }
  116. void NewsDialog::proxySettingsButton_clicked()
  117. {
  118. KCModuleInfo proxyModuleInfo("proxy");
  119. if (proxyModuleInfo.moduleName().isNull())
  120. KMessageBox::error(this,"This feature requires kdebase.");
  121. else {
  122. KCMultiDialog proxySettings(this);
  123. proxySettings.addModule(proxyModuleInfo);
  124. proxySettings.exec();
  125. }
  126. }
  127. void NewsDialog::refreshButton_clicked()
  128. {
  129. loadNews();
  130. }
  131. void NewsDialog::visitButton_clicked()
  132. {
  133. KRun::runUrl(KUrl("http://tigcc.ticalc.org/linux/"),"text/html",
  134. static_cast<QWidget *>(parent()));
  135. }
  136. /*
  137. * Constructs a NewsDialog as a child of 'parent', with the
  138. * name 'name' and widget flags set to 'f'.
  139. *
  140. * The dialog will by default be modeless, unless you set 'modal' to
  141. * true to construct a modal dialog.
  142. */
  143. NewsDialog::NewsDialog(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
  144. : QDialog(parent, name, modal, fl)
  145. {
  146. setupUi(this);
  147. }
  148. /*
  149. * Destroys the object and frees any allocated resources
  150. */
  151. NewsDialog::~NewsDialog()
  152. {
  153. // no need to delete child widgets, Qt does it all for us
  154. }
  155. /*
  156. * Sets the strings of the subwidgets using the current
  157. * language.
  158. */
  159. void NewsDialog::languageChange()
  160. {
  161. retranslateUi(this);
  162. }