annotate libgui/src/news-reader.cc @ 27613:deb8877f2c34

use symbolic constants for preferences key and default values in news reader * gui-preferences-all.h: added new include file for news reader * main-window.cc: include new include file for news reader (main_window, load_and_display_community_news): use symbolic constants for news reader preferences * module.mk: added new include file for news reader * news-reader.cc: include new include file for news reader (process): use symbolic constants for news reader preferences * settings-dialog.cc (settings_dialog, write_changed_settings): use symbolic constants for news reader preferences * welcome-wizard.cc (handle_web_connect_option): use symbolic constants for news reader preferences; (setup_community_news): use default value for presetting the checkbox for internet connection
author Torsten Lilge <ttl-octave@mailbox.org>
date Sat, 02 Nov 2019 07:41:42 +0100
parents 0495b64288f7
children 45bb5bbaf291
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 /*
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 Copyright (C) 2013-2019 John W. Eaton
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 Copyright (C) 2011-2019 Jacob Dawid
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 This file is part of Octave.
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 Octave is free software: you can redistribute it and/or modify it
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 (at your option) any later version.
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 WITHOUT ANY WARRANTY; without even the implied warranty of
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 GNU General Public License for more details.
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 <https://www.gnu.org/licenses/>.
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 */
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 #if defined (HAVE_CONFIG_H)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 # include "config.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #endif
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 #include <string>
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 #include <QDateTime>
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 #include <QString>
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 #include "news-reader.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 #include "resource-manager.h"
27613
deb8877f2c34 use symbolic constants for preferences key and default values in news reader
Torsten Lilge <ttl-octave@mailbox.org>
parents: 27611
diff changeset
35 #include "gui-preferences-nr.h"
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 #include "url-transfer.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 #include "version.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 namespace octave
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 void news_reader::process (void)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 QString html_text;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 if (m_connect_to_web)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 // Run this part in a separate thread so Octave can continue to
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 // run while we wait for the page to load. Then emit the signal
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 // to display it when we have the page contents.
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 QString url = m_base_url + '/' + m_page;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 std::ostringstream buf;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 url_transfer octave_dot_org (url.toStdString (), buf);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 if (octave_dot_org.is_valid ())
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 Array<std::string> param;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 octave_dot_org.http_get (param);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 if (octave_dot_org.good ())
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 html_text = QString::fromStdString (buf.str ());
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 if (html_text.contains ("this-is-the-gnu-octave-community-news-page"))
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 if (m_serial >= 0)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 {
27611
0495b64288f7 use new gui_settings class instead of using QSettings directly
John W. Eaton <jwe@octave.org>
parents: 27193
diff changeset
69 gui_settings *settings = resource_manager::get_settings ();
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 if (settings)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 {
27613
deb8877f2c34 use symbolic constants for preferences key and default values in news reader
Torsten Lilge <ttl-octave@mailbox.org>
parents: 27611
diff changeset
73 settings->setValue (nr_last_time.key,
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 QDateTime::currentDateTime ());
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 settings->sync ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 QString tag ("community-news-page-serial=");
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 int b = html_text.indexOf (tag);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 if (b)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 b += tag.length ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 int e = html_text.indexOf ("\n", b);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 QString tmp = html_text.mid (b, e-b);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 int curr_page_serial = tmp.toInt ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 if (curr_page_serial > m_serial)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 if (settings)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 {
27613
deb8877f2c34 use symbolic constants for preferences key and default values in news reader
Torsten Lilge <ttl-octave@mailbox.org>
parents: 27611
diff changeset
97 settings->setValue (nr_last_news.key,
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98 curr_page_serial);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 settings->sync ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 return;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 return;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 html_text = QString
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 (tr ("<html>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 "<body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 "Octave's community news source seems to be unavailable.\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 "For the latest news, please check\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 "<a href=\"https://octave.org/community-news.html\">https://octave.org/community-news.html</a>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 "when you have a connection to the web (link opens in an external browser).\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123 "<small><em>&mdash; The Octave Developers, ") + OCTAVE_RELEASE_DATE + "</em></small>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
124 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
125 "</body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
126 "</html>\n");
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
127 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
128 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
129 html_text = QString
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
130 (tr ("<html>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
131 "<body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
132 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
133 "Connecting to the web to display the latest Octave Community news has been disabled.\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
134 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
135 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
136 "For the latest news, please check\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
137 "<a href=\"https://octave.org/community-news.html\">https://octave.org/community-news.html</a>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
138 "when you have a connection to the web (link opens in an external browser)\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
139 "or enable web connections for news in Octave's network settings dialog.\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
140 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
141 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
142 "<small><em>&mdash; The Octave Developers, ") + OCTAVE_RELEASE_DATE + "</em></small>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
143 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
144 "</body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
145 "</html>\n");
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
146
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
147 emit display_news_signal (html_text);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
148
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
149 emit finished ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
150 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
151 }