newsdlg.cpp 5.1 KB

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