newsdlg.cpp 4.7 KB

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