Skip to content

Commit d921285

Browse files
committed
preparations for opening a HTML file after HTML generation
1 parent af2d89d commit d921285

File tree

9 files changed

+224
-0
lines changed

9 files changed

+224
-0
lines changed

code/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(pmdb_sources
2929
bbcode/TableBBCode.cpp
3030
bbcode/TableClasses.cpp
3131
bbcode/quotes.cpp
32+
browser_detection.cpp
3233
filters/FilterUser.cpp
3334
functions.cpp
3435
html_generation.cpp

code/browser_detection.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#include "browser_detection.hpp"
22+
23+
std::optional<BrowserInformation> detect_browser()
24+
{
25+
#ifdef _WIN32
26+
// TODO: Not implemented yet.
27+
return std::optional<BrowserInformation>();
28+
#else
29+
// On Linux systems the paths are usually similar, depending on how the
30+
// browser was packaged. So it's easiest to just test some known paths.
31+
const std::vector<BrowserInformation> known_paths = {
32+
{ "/usr/bin/firefox", Browser::Firefox },
33+
{ "/usr/bin/firefox-esr", Browser::Firefox },
34+
{ "/usr/bin/firefox-devedition", Browser::Firefox },
35+
{ "/usr/bin/chromium", Browser::Chromium },
36+
{ "/usr/bin/chromium-browser", Browser::Chromium },
37+
};
38+
39+
for (const auto& element: known_paths)
40+
{
41+
std::error_code error;
42+
if (std::filesystem::is_regular_file(element.path, error) && !error)
43+
{
44+
return element;
45+
}
46+
}
47+
48+
// No known browser found.
49+
return std::optional<BrowserInformation>();
50+
#endif
51+
}
52+
53+
std::vector<std::string> additional_parameters(const Browser type)
54+
{
55+
switch(type)
56+
{
57+
case Browser::Firefox:
58+
return { "--private-window" };
59+
case Browser::Chromium:
60+
return { "--incognito" };
61+
default:
62+
return {};
63+
}
64+
}

code/browser_detection.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#ifndef PMDB_BROWSER_DETECTION_HPP
22+
#define PMDB_BROWSER_DETECTION_HPP
23+
24+
#include <filesystem>
25+
#include <optional>
26+
#include <string>
27+
#include <vector>
28+
29+
/// enumeration for browser type
30+
enum class Browser
31+
{
32+
/// Firefox browser or compatible forks
33+
Firefox,
34+
35+
/// Chromium browser
36+
Chromium
37+
};
38+
39+
struct BrowserInformation
40+
{
41+
std::filesystem::path path; /**< path to executable */
42+
Browser type; /**< general browser type */
43+
};
44+
45+
/** \brief Detects an available web browser.
46+
*
47+
* \return Returns information about a web browser, if one was found.
48+
Returns an empty optional, if no browser was found.
49+
* \remark If the system has more than one browser installed, the function will
50+
* just return information about one of them, usually the first that it
51+
* can find.
52+
*/
53+
std::optional<BrowserInformation> detect_browser();
54+
55+
/** \brief Get additional parameters for browser executable.
56+
*
57+
* \param type the type of browser
58+
* \return Returns a vector of parameters for the browser executable.
59+
*/
60+
std::vector<std::string> additional_parameters(const Browser type);
61+
62+
#endif // PMDB_BROWSER_DETECTION_HPP

code/html_generation.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "html_generation.hpp"
2222
#include <fstream>
2323
#include <iostream>
24+
#include "browser_detection.hpp"
2425
#include "Config.hpp"
2526
#include "paths.hpp"
2627
#include "ReturnCodes.hpp"
@@ -33,6 +34,38 @@
3334
#include "templates/functions.hpp"
3435
#include "../libstriezel/filesystem/directory.hpp"
3536
#include "../libstriezel/filesystem/file.hpp"
37+
#include "../libstriezel/hash/sha256/BufferSourceUtility.hpp"
38+
39+
std::string getFirstFolderFileName(const FolderMap& fm)
40+
{
41+
const auto folders = fm.getPresentFolders();
42+
if (folders.empty())
43+
{
44+
return "";
45+
}
46+
const std::string& name = *folders.begin();
47+
return "folder_"
48+
+ SHA256::computeFromBuffer((uint8_t*) name.c_str(), name.length() * 8).toHexString()
49+
+ ".html";
50+
}
51+
52+
void openFirstIndexFile(const FolderMap& fm, const std::string& html_dir)
53+
{
54+
auto fileName = getFirstFolderFileName(fm);
55+
if (fileName.empty())
56+
{
57+
return;
58+
}
59+
const auto fullFileName = html_dir + fileName;
60+
const auto browser = detect_browser();
61+
if (!browser.has_value())
62+
{
63+
std::cout << "Info: Could not find a browser to open generated files.\n"
64+
<< "Info: Open " << fullFileName << " in a browser to see the generated HTML files.\n";
65+
return;
66+
}
67+
// TODO: Open file via Boost Process.
68+
}
3669

3770
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions)
3871
{
@@ -209,5 +242,7 @@ int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTM
209242
return rcFileError;
210243
}
211244
std::cout << "All HTML files were created successfully!\n";
245+
// Open first HTML file in browser.
246+
openFirstIndexFile(fm, htmlDir);
212247
return 0;
213248
}

code/pmdb.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
<Unit filename="bbcode/TextProcessor.hpp" />
147147
<Unit filename="bbcode/quotes.cpp" />
148148
<Unit filename="bbcode/quotes.hpp" />
149+
<Unit filename="browser_detection.cpp" />
150+
<Unit filename="browser_detection.hpp" />
149151
<Unit filename="filters/Filter.hpp" />
150152
<Unit filename="filters/FilterUser.cpp" />
151153
<Unit filename="filters/FilterUser.hpp" />

program-no-compression/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ set(pmdb_no_comp_sources
3434
../code/bbcode/TableBBCode.cpp
3535
../code/bbcode/TableClasses.cpp
3636
../code/bbcode/quotes.cpp
37+
../code/browser_detection.cpp
3738
../code/filters/FilterUser.cpp
3839
../code/functions.cpp
3940
../code/html_generation.cpp

tests/components/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(component_tests_sources
3030
../../code/bbcode/TableClasses.cpp
3131
../../code/bbcode/TextProcessor.hpp
3232
../../code/bbcode/quotes.cpp
33+
../../code/browser_detection.cpp
3334
../../code/filters/FilterUser.cpp
3435
../../code/paths.cpp
3536
../../code/templates/defaults.hpp
@@ -68,6 +69,7 @@ set(component_tests_sources
6869
bbcode/TableBBCode.cpp
6970
bbcode/TablePreProcessor.cpp
7071
bbcode/quotes.cpp
72+
browser_detection.cpp
7173
filter/FilterUser.cpp
7274
names_to_controlsequences.cpp
7375
paths.cpp
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database test suite.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#include "../locate_catch.hpp"
22+
#include "../../code/browser_detection.hpp"
23+
24+
TEST_CASE("browser detection")
25+
{
26+
SECTION("detect_browser")
27+
{
28+
const auto info = detect_browser();
29+
#if _WIN32
30+
// TODO: Change this as soon as detect_browser() works on Windows, too.
31+
REQUIRE_FALSE( info.has_value() );
32+
#else
33+
REQUIRE( info.has_value() );
34+
REQUIRE_FALSE( info.value().path.empty() );
35+
#endif
36+
}
37+
38+
SECTION("additional parameters")
39+
{
40+
SECTION("Chromium")
41+
{
42+
const auto params = additional_parameters(Browser::Chromium);
43+
REQUIRE( params.size() == 1 );
44+
REQUIRE( params[0] == "--incognito" );
45+
}
46+
47+
SECTION("Firefox")
48+
{
49+
const auto params = additional_parameters(Browser::Firefox);
50+
REQUIRE( params.size() == 1 );
51+
REQUIRE( params[0] == "--private-window" );
52+
}
53+
}
54+
}

tests/components/component_tests.cbp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
<Unit filename="../../code/bbcode/TextProcessor.hpp" />
8686
<Unit filename="../../code/bbcode/quotes.cpp" />
8787
<Unit filename="../../code/bbcode/quotes.hpp" />
88+
<Unit filename="../../code/browser_detection.cpp" />
89+
<Unit filename="../../code/browser_detection.hpp" />
8890
<Unit filename="../../code/filters/Filter.hpp" />
8991
<Unit filename="../../code/filters/FilterUser.cpp" />
9092
<Unit filename="../../code/filters/FilterUser.hpp" />
@@ -137,6 +139,7 @@
137139
<Unit filename="bbcode/TableBBCode.cpp" />
138140
<Unit filename="bbcode/TablePreProcessor.cpp" />
139141
<Unit filename="bbcode/quotes.cpp" />
142+
<Unit filename="browser_detection.cpp" />
140143
<Unit filename="filter/FilterUser.cpp" />
141144
<Unit filename="main.cpp" />
142145
<Unit filename="names_to_controlsequences.cpp" />

0 commit comments

Comments
 (0)