Skip to content

Commit 2218069

Browse files
committed
move methods to open file into separate files
That way the HTML generation code and the code to open a HTML file are separated.
1 parent 570734a commit 2218069

File tree

8 files changed

+129
-75
lines changed

8 files changed

+129
-75
lines changed

code/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(pmdb_sources
3333
filters/FilterUser.cpp
3434
functions.cpp
3535
html_generation.cpp
36+
open_file.cpp
3637
paths.cpp
3738
templates/defaults.hpp
3839
templates/functions.cpp

code/html_generation.cpp

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@
2121
#include "html_generation.hpp"
2222
#include <fstream>
2323
#include <iostream>
24-
#include <boost/version.hpp>
25-
#if (BOOST_VERSION <= 108100) || defined(USE_BOOST_PROCESS_V1)
26-
#include <boost/process.hpp>
27-
#else
28-
#include <boost/asio/io_context.hpp>
29-
#include <boost/process/v2/process.hpp>
30-
#if defined(_WIN32)
31-
#include <boost/process/v2/windows/default_launcher.hpp>
32-
#else
33-
#include <boost/process/v2/posix/fork_and_forget_launcher.hpp>
34-
#endif
35-
#endif
36-
#include "browser_detection.hpp"
3724
#include "Config.hpp"
3825
#include "paths.hpp"
3926
#include "ReturnCodes.hpp"
@@ -46,61 +33,6 @@
4633
#include "templates/functions.hpp"
4734
#include "../libstriezel/filesystem/directory.hpp"
4835
#include "../libstriezel/filesystem/file.hpp"
49-
#include "../libstriezel/hash/sha256/BufferSourceUtility.hpp"
50-
51-
std::string getFirstFolderFileName(const FolderMap& fm)
52-
{
53-
const auto folders = fm.getPresentFolders();
54-
if (folders.empty())
55-
{
56-
return "";
57-
}
58-
const std::string& name = *folders.begin();
59-
return "folder_"
60-
+ SHA256::computeFromBuffer((uint8_t*) name.c_str(), name.length() * 8).toHexString()
61-
+ ".html";
62-
}
63-
64-
void openFirstIndexFile(const FolderMap& fm, const std::string& html_dir)
65-
{
66-
const auto fileName = getFirstFolderFileName(fm);
67-
if (fileName.empty())
68-
{
69-
return;
70-
}
71-
const auto fullFileName = libstriezel::filesystem::slashify(html_dir) + fileName;
72-
const auto browser = detect_browser();
73-
if (!browser.has_value())
74-
{
75-
std::cout << "Info: Could not find a browser to open generated files.\n"
76-
<< "Info: Open " << fullFileName << " in a browser to see the generated HTML files.\n";
77-
return;
78-
}
79-
// Open file via Boost Process.
80-
std::cout << "Opening " << fullFileName << " in browser ...\n";
81-
std::vector<std::string> params = additional_parameters(browser.value().type);
82-
#if (BOOST_VERSION <= 108100) || defined(USE_BOOST_PROCESS_V1)
83-
std::string command = browser.value().path.string();
84-
for (const auto& param: params)
85-
{
86-
command += " " + param;
87-
}
88-
command += " " + fullFileName;
89-
boost::process::child child(command);
90-
child.detach();
91-
#else
92-
// After Boost 1.81.0 use process v2 API.
93-
boost::asio::io_context context;
94-
params.push_back(fullFileName);
95-
#if defined(_WIN32)
96-
auto launcher = boost::process::v2::windows::default_launcher();
97-
#else
98-
auto launcher = boost::process::v2::posix::fork_and_forget_launcher();
99-
#endif
100-
boost::process::v2::process proc(context, browser.value().path.string(), params);
101-
proc.detach();
102-
#endif
103-
}
10436

10537
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions)
10638
{

code/html_generation.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,4 @@
3535
*/
3636
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions);
3737

38-
/** \brief Attempts to open the first folder index HTML file in a browser.
39-
*
40-
* \param fm folder mappings for the message database
41-
* \param html_dir directory where the HTML files reside
42-
*/
43-
void openFirstIndexFile(const FolderMap& fm, const std::string& html_dir);
44-
4538
#endif // PMDB_HTML_GENERATION_HPP

code/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "functions.hpp"
3636
#include "html_generation.hpp"
3737
#include "HTMLOptions.hpp"
38+
#include "open_file.hpp"
3839
#include "ReturnCodes.hpp"
3940
#include "../libstriezel/filesystem/directory.hpp"
4041
#include "../libstriezel/filesystem/file.hpp"

code/open_file.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 "open_file.hpp"
22+
#include <iostream>
23+
#include <boost/version.hpp>
24+
#if (BOOST_VERSION <= 108100) || defined(USE_BOOST_PROCESS_V1)
25+
#include <boost/process.hpp>
26+
#else
27+
#include <boost/asio/io_context.hpp>
28+
#include <boost/process/v2/process.hpp>
29+
#if defined(_WIN32)
30+
#include <boost/process/v2/windows/default_launcher.hpp>
31+
#else
32+
#include <boost/process/v2/posix/fork_and_forget_launcher.hpp>
33+
#endif
34+
#endif
35+
#include "browser_detection.hpp"
36+
#include "../libstriezel/filesystem/directory.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+
const auto fileName = getFirstFolderFileName(fm);
55+
if (fileName.empty())
56+
{
57+
return;
58+
}
59+
const auto fullFileName = libstriezel::filesystem::slashify(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+
// Open file via Boost Process.
68+
std::cout << "Opening " << fullFileName << " in browser ...\n";
69+
std::vector<std::string> params = additional_parameters(browser.value().type);
70+
#if (BOOST_VERSION <= 108100) || defined(USE_BOOST_PROCESS_V1)
71+
std::string command = browser.value().path.string();
72+
for (const auto& param: params)
73+
{
74+
command += " " + param;
75+
}
76+
command += " " + fullFileName;
77+
boost::process::child child(command);
78+
child.detach();
79+
#else
80+
// After Boost 1.81.0 use process v2 API.
81+
boost::asio::io_context context;
82+
params.push_back(fullFileName);
83+
#if defined(_WIN32)
84+
auto launcher = boost::process::v2::windows::default_launcher();
85+
#else
86+
auto launcher = boost::process::v2::posix::fork_and_forget_launcher();
87+
#endif
88+
boost::process::v2::process proc(context, browser.value().path.string(), params);
89+
proc.detach();
90+
#endif
91+
}

code/open_file.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_OPEN_FILE_HPP
22+
#define PMDB_OPEN_FILE_HPP
23+
24+
#include "FolderMap.hpp"
25+
26+
/** \brief Attempts to open the first folder index HTML file in a browser.
27+
*
28+
* \param fm folder mappings for the message database
29+
* \param html_dir directory where the HTML files reside
30+
*/
31+
void openFirstIndexFile(const FolderMap& fm, const std::string& html_dir);
32+
33+
#endif // PMDB_OPEN_FILE_HPP

code/pmdb.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@
156156
<Unit filename="html_generation.cpp" />
157157
<Unit filename="html_generation.hpp" />
158158
<Unit filename="main.cpp" />
159+
<Unit filename="open_file.cpp" />
160+
<Unit filename="open_file.hpp" />
159161
<Unit filename="paths.cpp" />
160162
<Unit filename="paths.hpp" />
161163
<Unit filename="templates/defaults.hpp" />

program-no-compression/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(pmdb_no_comp_sources
3838
../code/filters/FilterUser.cpp
3939
../code/functions.cpp
4040
../code/html_generation.cpp
41+
../code/open_file.cpp
4142
../code/paths.cpp
4243
../code/templates/defaults.hpp
4344
../code/templates/functions.cpp

0 commit comments

Comments
 (0)