newsdlg.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <kconfiggroup.h>
  25. #include <kpushbutton.h>
  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. NewsDialog::NewsDialog(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
  57. : QDialog(parent, name, modal, fl)
  58. {
  59. setupUi(this);
  60. closeButton->setGuiItem(KStandardGuiItem::Close);
  61. }
  62. NewsDialog::~NewsDialog()
  63. {
  64. }
  65. bool NewsDialog::loadNews()
  66. {
  67. QString tmpFile;
  68. bool result=FALSE;
  69. KConfigGroup config=pconfig->group("News Headlines");
  70. QDate latestHeadline=config.readEntry("Latest Headline",QDateTime()).date();
  71. if(KIO::NetAccess::download(
  72. KUrl("http://tigcc.ticalc.org/linux/newsheadlines.txt"),tmpFile,this)) {
  73. #define ERROR(s) do {KMessageBox::error(this,(s)); goto done;} while(0)
  74. #define ZAP_LF() do {char *p=line+(std::strlen(line)-1); if (*p=='\n') *p=0;} while(0)
  75. #define ZAP_CR() do {char *p=line+(std::strlen(line)-1); if (*p=='\r') *p=0;} while(0)
  76. std::FILE *f=std::fopen(tmpFile,"r");
  77. if (!f) ERROR("Downloading news failed.");
  78. char line[32768];
  79. // "TIGCC News Format"
  80. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  81. ZAP_LF();ZAP_CR();
  82. if (std::strcmp(line,"TIGCC News Format")) ERROR("Invalid news file.");
  83. // Empty line
  84. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  85. ZAP_LF();ZAP_CR();
  86. if (*line) ERROR("Invalid news file.");
  87. newsListBox->clear();
  88. while (1) {
  89. bool itemIsNew=FALSE;
  90. unsigned y,m,d;
  91. // Date
  92. if (!std::fgets(line,32768,f)) goto done;
  93. ZAP_LF();ZAP_CR();
  94. if (!*line) goto done;
  95. if (std::sscanf(line,"%4u%2u%2u",&y,&m,&d)<3) ERROR("Invalid news file.");
  96. if (latestHeadline.isNull() || QDate(y,m,d)>latestHeadline) {
  97. if (!result) {
  98. config.writeEntry("Latest Headline",QDateTime(QDate(y,m,d)));
  99. pconfig->sync();
  100. }
  101. result=itemIsNew=TRUE;
  102. }
  103. // Title
  104. if (!std::fgets(line,32768,f)) ERROR("Invalid news file.");
  105. ZAP_LF();ZAP_CR();
  106. new ColoredListBoxText(newsListBox,QString::fromUtf8(line),
  107. itemIsNew?Qt::red:Qt::gray);
  108. // Empty line
  109. if (!std::fgets(line,32768,f)) goto done;
  110. ZAP_LF();ZAP_CR();
  111. if (*line) ERROR("Invalid news file.");
  112. }
  113. #undef ERROR
  114. #undef ZAP_LF
  115. #undef ZAP_CR
  116. done: if (f) std::fclose(f);
  117. KIO::NetAccess::removeTempFile(tmpFile);
  118. } else {
  119. KMessageBox::error(this,KIO::NetAccess::lastErrorString());
  120. }
  121. return result;
  122. }
  123. void NewsDialog::proxySettingsButton_clicked()
  124. {
  125. KCModuleInfo proxyModuleInfo("proxy");
  126. if (proxyModuleInfo.moduleName().isNull())
  127. KMessageBox::error(this,"This feature requires kdebase.");
  128. else {
  129. KCMultiDialog proxySettings(this);
  130. proxySettings.addModule(proxyModuleInfo);
  131. proxySettings.exec();
  132. }
  133. }
  134. void NewsDialog::refreshButton_clicked()
  135. {
  136. loadNews();
  137. }
  138. void NewsDialog::visitButton_clicked()
  139. {
  140. KRun::runUrl(KUrl("http://tigcc.ticalc.org/linux/"),"text/html",
  141. static_cast<QWidget *>(parent()));
  142. }
  143. void NewsDialog::languageChange()
  144. {
  145. retranslateUi(this);
  146. }