"Fossies" - the Fresh Open Source Software Archive 
Member "cb2bib-2.0.1/src/c2bNetworkQueryInfo.cpp" (12 Feb 2021, 6601 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "c2bNetworkQueryInfo.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.0.0_vs_2.0.1.
1 /***************************************************************************
2 * Copyright (C) 2004-2021 by Pere Constans
3 * constans@molspaces.com
4 * cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
5 * See the LICENSE file that comes with this distribution.
6 ***************************************************************************/
7 #include "c2bNetworkQueryInfo.h"
8
9 #include "c2b.h"
10 #include "c2bBibParser.h"
11 #include "c2bFileSystemWatcher.h"
12 #include "c2bSettings.h"
13 #include "c2bUtils.h"
14
15 #include <QAction>
16 #include <QTextStream>
17 #include <QUrl>
18
19
20 c2bNetworkQueryInfo::c2bNetworkQueryInfo(QObject* parento) : QObject(parento)
21 {
22 _bpP = c2b::instance()->bibParser();
23 connect(c2bSettingsP, SIGNAL(newSettings()), this, SLOT(loadSettings()));
24 _fsw = new c2bFileSystemWatcher(this);
25 connect(_fsw, SIGNAL(fileChanged(QString)), this, SLOT(parseBookmarks()));
26 loadSettings();
27 }
28
29 c2bNetworkQueryInfo::~c2bNetworkQueryInfo() {}
30
31
32 void c2bNetworkQueryInfo::loadSettings()
33 {
34 const QString nfn(c2bSettingsP->fileName("cb2Bib/NetworkFile"));
35 if (nfn == NetworkFile)
36 return;
37 if (QFileInfo::exists(nfn))
38 {
39 if (!NetworkFile.isEmpty())
40 _fsw->removePath(NetworkFile);
41 NetworkFile = nfn;
42 _fsw->addPath(NetworkFile);
43 }
44 parseBookmarks();
45 }
46
47 void c2bNetworkQueryInfo::parseBookmarks()
48 {
49 BookmarkList.clear();
50 if (NetworkFile.isEmpty())
51 return;
52 QFile file(NetworkFile);
53 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
54 return;
55 QRegExp _bookmark("^bookmark=");
56 QString line;
57 QTextStream stream(&file);
58 stream.setCodec("UTF-8");
59 stream.setAutoDetectUnicode(true);
60 while (!stream.atEnd())
61 {
62 line = stream.readLine();
63 if (line.contains(_bookmark))
64 {
65 line.remove(_bookmark);
66 if (line.count('|') == 0)
67 c2bUtils::warn(tr("c2bNetworkQueryInfo::parseBookmarks: Syntax error at '%1'").arg(line));
68 else
69 BookmarkList.append(line);
70 }
71 }
72 file.close();
73 while (!BookmarkActionList.isEmpty())
74 delete BookmarkActionList.takeFirst();
75 const QRegExp rx("<<.+>>");
76 for (int i = 0; i < BookmarkList.count(); ++i)
77 if (BookmarkList.at(i).contains(rx))
78 {
79 const QStringList bm_parts(BookmarkList.at(i).split('|'));
80 if (bm_parts.count() > 1)
81 {
82 QAction* act = new QAction(bm_parts.at(0), this);
83 act->setData(bm_parts.at(1));
84 act->setStatusTip(QString("%1 [%2]").arg(bm_parts.at(0), bm_parts.at(1)));
85 connect(act, SIGNAL(triggered()), this, SLOT(openBookmark()));
86 BookmarkActionList.append(act);
87 }
88 }
89 }
90
91 QList<QAction*> c2bNetworkQueryInfo::bookmarkActions(const QString& selection)
92 {
93 _current_selection = selection;
94 for (int i = 0; i < BookmarkActionList.count(); ++i)
95 BookmarkActionList[i]->setVisible(false);
96 const QRegExp rx("<<.+>>");
97 for (int i = 0; i < BookmarkActionList.count(); ++i)
98 {
99 QString bmd(BookmarkActionList.at(i)->data().toString());
100 const QStringList& bibliographicFields = _bpP->bibliographicFields();
101 for (int j = 0; j < bibliographicFields.count(); ++j)
102 {
103 const QString& name = bibliographicFields.at(j);
104 const QString tag(QString("<<%1>>").arg(name));
105 if (bmd.contains(tag))
106 if (!_bpP->field(name).isEmpty())
107 bmd.remove(tag);
108 }
109 if (bmd.contains("<<selection>>"))
110 if (!_current_selection.isEmpty())
111 bmd.remove("<<selection>>");
112 BookmarkActionList[i]->setVisible(!bmd.contains(rx));
113 }
114 return BookmarkActionList;
115 }
116
117 void c2bNetworkQueryInfo::openBookmark()
118 {
119 QAction* act = static_cast<QAction*>(sender());
120 if (act)
121 {
122 const QString link(act->data().toString());
123 c2bUtils::openFile(encodeLink(link), c2b::mainWidget());
124 }
125 }
126
127 const QString c2bNetworkQueryInfo::encodeLink(const QString& ln) const
128 {
129 QString eln(ln);
130 // See comment in c2bNetworkQueryInfo::bookmarksToHtml
131 eln.replace("[[[", "<<");
132 eln.replace("]]]", ">>");
133 const QRegExp rx("<<.+>>");
134 if (eln.contains(rx))
135 {
136 const QStringList& bibliographicFields = _bpP->bibliographicFields();
137 for (int j = 0; j < bibliographicFields.count(); ++j)
138 {
139 const QString& name = bibliographicFields.at(j);
140 const QString tag(QString("<<%1>>").arg(name));
141 if (eln.contains(tag))
142 {
143 QString fld;
144 if (name == "pages")
145 fld = _bpP->field(name).replace(QRegExp("-.+$"), QString());
146 else if (name == "author")
147 {
148 fld = _bpP->field(name);
149 fld.replace(" and ", " ");
150 }
151 else if (name == "title")
152 {
153 fld = _bpP->field(name);
154 fld = fld.remove('{');
155 fld = fld.remove('}');
156 }
157 else
158 fld = _bpP->field(name);
159 fld = QUrl::toPercentEncoding(fld);
160 eln.replace(tag, fld);
161 if (!eln.contains(rx))
162 return eln;
163 }
164 }
165 eln.replace("<<selection>>", _current_selection);
166 }
167 return eln;
168 }
169
170 const QString c2bNetworkQueryInfo::bookmarksToHtml() const
171 {
172 if (BookmarkList.count() == 0)
173 return QString();
174 QString html("<p align=\"center\">Bookmarks:<br /><p align=\"center\"><em>");
175 for (int i = 0; i < BookmarkList.count(); ++i)
176 {
177 int separator(BookmarkList.at(i).indexOf('|'));
178 if (separator == -1)
179 continue;
180 QString url_name(BookmarkList.at(i).mid(0, separator));
181 QString url(BookmarkList.at(i).mid(separator + 1));
182 // Tags <<.>> conflict with the Qt > 4.4.x HTML parser, even being them inside a
183 // literal, quoted string. The substitution below works as a workaround to prevent
184 // cb2Bib tags from clashing with the parser.
185 // See c2bNetworkQueryInfo::encodeLink. A similar issue was resolved in
186 // fromQtXmlString/toQtXmlString.
187 url.replace("<<", "[[[");
188 url.replace(">>", "]]]");
189 url = "<a href=\"" + url + "\">" + url_name + "</a>";
190 html += url + " <br />";
191 }
192 html += "</em></p><br />";
193 return html;
194 }