newsdlg.cpp 5.1 KB

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