annotate libgui/src/news-reader.cc @ 31649:deb553ac2c54

maint: Merge stable to default.
author John W. Eaton <jwe@octave.org>
date Tue, 06 Dec 2022 15:45:27 -0500
parents 431f80aba37a 29d734430e5f
children aae24eaf89ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
3 // Copyright (C) 2011-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #if defined (HAVE_CONFIG_H)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 # include "config.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 #endif
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 <string>
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 #include <QDateTime>
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 #include <QString>
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 #include "news-reader.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
36 #include "gui-preferences-nr.h"
31619
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
37 #include "gui-settings.h"
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 #include "url-transfer.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 #include "version.h"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
31646
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
42 OCTAVE_BEGIN_NAMESPACE(octave)
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
43
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 void news_reader::process (void)
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 QString html_text;
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 if (m_connect_to_web)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 // 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
51 // 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
52 // 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
53
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 QString url = m_base_url + '/' + m_page;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 std::ostringstream buf;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 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
57
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 if (octave_dot_org.is_valid ())
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 Array<std::string> param;
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 octave_dot_org.http_get (param);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 if (octave_dot_org.good ())
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 html_text = QString::fromStdString (buf.str ());
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 }
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 (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
68 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 if (m_serial >= 0)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 {
31619
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
71 gui_settings settings;
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
31619
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
73 settings.setValue (nr_last_time.key,
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
74 QDateTime::currentDateTime ());
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75
31619
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
76 settings.sync ();
27193
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 QString tag ("community-news-page-serial=");
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 int b = html_text.indexOf (tag);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 if (b)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 {
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 b += tag.length ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 int e = html_text.indexOf ("\n", b);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 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
89
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 int curr_page_serial = tmp.toInt ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 if (curr_page_serial > m_serial)
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 {
31619
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
94 settings.setValue (nr_last_news.key, curr_page_serial);
ad014fc78bd6 use individual local gui_settings objects
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
95 settings.sync ();
27193
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98 return;
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 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 return;
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 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 html_text = QString
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 (tr ("<html>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 "<body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 "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
110 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 "For the latest news, please check\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 "<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
114 "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
115 "</p>\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 "<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
118 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 "</body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 "</html>\n");
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 }
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 else
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123 html_text = QString
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
124 (tr ("<html>\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 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
127 "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
128 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
129 "<p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
130 "For the latest news, please check\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
131 "<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
132 "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
133 "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
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 "<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
137 "</p>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
138 "</body>\n"
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
139 "</html>\n");
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
140
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
141 emit display_news_signal (html_text);
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
142
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
143 emit finished ();
01e73e1664ff move news reader widget to separate file
John W. Eaton <jwe@octave.org>
parents:
diff changeset
144 }
31646
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
145
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
146 OCTAVE_END_NAMESPACE(octave)