newsdlg.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <qvariant.h>
  24. #include <qimage.h>
  25. #include <qpixmap.h>
  26. #include <qdatetime.h>
  27. #include <qstring.h>
  28. #include <q3listbox.h>
  29. #include <qcolor.h>
  30. #include <qpainter.h>
  31. #include <qpen.h>
  32. #include <cstdio>
  33. #include <cstring>
  34. #include "ktigcc.h"
  35. class ColoredListBoxText : public Q3ListBoxText {
  36. public:
  37. ColoredListBoxText(Q3ListBox *listbox, const QString &text,
  38. const QColor &textColor)
  39. : Q3ListBoxText(listbox,text), color(textColor) {}
  40. virtual ~ColoredListBoxText() {}
  41. protected:
  42. virtual void paint(QPainter *painter) {
  43. QPen oldPen=painter->pen();
  44. QPen pen=oldPen;
  45. pen.setColor(color);
  46. painter->setPen(pen);
  47. Q3ListBoxText::paint(painter);
  48. painter->setPen(oldPen);
  49. }
  50. private:
  51. QColor color;
  52. };
  53. // This hack overrides the comment so it doesn't get translated while
  54. // everything else doesn't.
  55. class ProxyModuleInfo : public KCModuleInfo {
  56. public:
  57. ProxyModuleInfo() : KCModuleInfo("proxy") {
  58. setComment("Configure the proxy servers used");
  59. }
  60. };
  61. bool NewsDialog::loadNews()
  62. {
  63. QString tmpFile;
  64. bool result=FALSE;
  65. pconfig->setGroup("News Headlines");
  66. QDateTime defaultDateTime;
  67. QDate latestHeadline=pconfig->readDateTimeEntry("Latest Headline",
  68. &defaultDateTime).date();
  69. if(KIO::NetAccess::download(
  70. KUrl("http://tigcc.ticalc.org/linux/newsheadlines.txt"),tmpFile,this)) {
  71. #define ERROR(s) do {KMessageBox::error(this,(s)); goto done;} while(0)
  72. #define ZAP_LF() do {char *p=line+(std::strlen(line)-1); if (*p=='\n') *p=0;} while(0)
  73. #define ZAP_CR() do {char *p=line+(std::strlen(line)-1); if (*p=='\r') *p=0;} while(0)
  74. std::FILE *f=std::fopen(tmpFile,"r");
  75. if (!f) ERROR("Downloading news failed.");
  76. char line[32768];
  77. // "TIGCC News Format"
  78. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  79. ZAP_LF();ZAP_CR();
  80. if (std::strcmp(line,"TIGCC News Format")) ERROR("Invalid news file.");
  81. // Empty line
  82. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  83. ZAP_LF();ZAP_CR();
  84. if (*line) ERROR("Invalid news file.");
  85. newsListBox->clear();
  86. while (1) {
  87. bool itemIsNew=FALSE;
  88. unsigned y,m,d;
  89. // Date
  90. if (!std::fgets(line,32768,f)) goto done;
  91. ZAP_LF();ZAP_CR();
  92. if (!*line) goto done;
  93. if (std::sscanf(line,"%4u%2u%2u",&y,&m,&d)<3) ERROR("Invalid news file.");
  94. if (latestHeadline.isNull() || QDate(y,m,d)>latestHeadline) {
  95. if (!result) {
  96. pconfig->writeEntry("Latest Headline",QDateTime(QDate(y,m,d)));
  97. pconfig->sync();
  98. }
  99. result=itemIsNew=TRUE;
  100. }
  101. // Title
  102. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  103. ZAP_LF();ZAP_CR();
  104. new ColoredListBoxText(newsListBox,QString::fromUtf8(line),
  105. itemIsNew?Qt::red:Qt::gray);
  106. // Empty line
  107. if (!std::fgets(line,32768,f)) goto done;
  108. ZAP_LF();ZAP_CR();
  109. if (*line) ERROR("Invalid news file.");
  110. }
  111. #undef ERROR
  112. #undef ZAP_LF
  113. #undef ZAP_CR
  114. done: if (f) std::fclose(f);
  115. KIO::NetAccess::removeTempFile(tmpFile);
  116. } else {
  117. KMessageBox::error(this,KIO::NetAccess::lastErrorString());
  118. }
  119. return result;
  120. }
  121. void NewsDialog::proxySettingsButton_clicked()
  122. {
  123. ProxyModuleInfo proxyModuleInfo;
  124. if (proxyModuleInfo.moduleName().isNull())
  125. KMessageBox::error(this,"This feature requires kdebase.");
  126. else {
  127. KCMultiDialog proxySettings(this);
  128. proxySettings.addModule(proxyModuleInfo);
  129. proxySettings.exec();
  130. }
  131. }
  132. void NewsDialog::refreshButton_clicked()
  133. {
  134. loadNews();
  135. }
  136. void NewsDialog::visitButton_clicked()
  137. {
  138. KRun::runURL(KUrl("http://tigcc.ticalc.org/linux/"),"text/html");
  139. }
  140. /*
  141. * Constructs a NewsDialog as a child of 'parent', with the
  142. * name 'name' and widget flags set to 'f'.
  143. *
  144. * The dialog will by default be modeless, unless you set 'modal' to
  145. * true to construct a modal dialog.
  146. */
  147. NewsDialog::NewsDialog(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
  148. : QDialog(parent, name, modal, fl)
  149. {
  150. setupUi(this);
  151. }
  152. /*
  153. * Destroys the object and frees any allocated resources
  154. */
  155. NewsDialog::~NewsDialog()
  156. {
  157. // no need to delete child widgets, Qt does it all for us
  158. }
  159. /*
  160. * Sets the strings of the subwidgets using the current
  161. * language.
  162. */
  163. void NewsDialog::languageChange()
  164. {
  165. retranslateUi(this);
  166. }