lib+tools: updated strings to support i18n translations
[barry.git] / src / configfilewin32.cc
blobd457424f0c571dcefc020bfa83bc7bfcc65d0320
1 ///
2 /// \file configfilewin32.cc
3 /// Barry configuration class, for one device PIN, using Win32 APIs
4 ///
6 /*
7 Copyright (C) 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)
8 Portions Copyright (C) 2012, RealVNC Ltd.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "configfile.h"
24 #include "error.h"
25 #include "r_message.h"
26 #include <iostream>
27 #include <iomanip>
28 #include <sstream>
29 #include <string.h>
30 #include <Shlobj.h>
31 #include <winbase.h>
33 // Returned by GetFileAttributes to indicate failure
34 #define INVALID_ATTRIB 0xFFFFFFFF
36 namespace Barry {
39 /// Creates a tar.gz filename using PIN + date + time + label.
40 /// Does not include any path, just returns a new filename.
41 std::string MakeBackupFilename(const Barry::Pin &pin,
42 const std::string &label)
44 using namespace std;
46 SYSTEMTIME st;
47 GetLocalTime(&st);
49 std::string fileLabel = label;
50 if( fileLabel.size() ) {
51 // prepend a hyphen
52 fileLabel.insert(fileLabel.begin(), '-');
54 // translate all spaces and slashes
55 for( size_t i = 0; i < fileLabel.size(); i++ ) {
56 if( fileLabel[i] == ' ' )
57 fileLabel[i] = '_';
58 else if( fileLabel[i] == '/' )
59 fileLabel[i] = '-';
60 else if( fileLabel[i] == '\\' )
61 fileLabel[i] = '-';
65 ostringstream tarfilename;
66 tarfilename << pin.Str() << "-"
67 << setw(4) << setfill('0') << st.wYear
68 << setw(2) << setfill('0') << st.wMonth
69 << setw(2) << setfill('0') << st.wDay
70 << "-"
71 << setw(2) << setfill('0') << st.wHour
72 << setw(2) << setfill('0') << st.wMinute
73 << setw(2) << setfill('0') << st.wSecond
74 << fileLabel
75 << ".tar.gz";
76 return tarfilename.str();
79 void ConfigFile::BuildFilename()
81 TCHAR dirName[MAX_PATH];
82 CHAR dirNameA[MAX_PATH];
83 if( !SHGetSpecialFolderPath(NULL, dirName, CSIDL_APPDATA, TRUE) ) {
84 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
86 if( WideCharToMultiByte(CP_ACP, 0, dirName, -1, dirNameA, MAX_PATH, NULL, NULL) <= 0 ) {
87 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
89 dirNameA[MAX_PATH-1] = 0; // Make sure it's NUL terminated
90 m_filename = dirNameA;
91 m_filename += "\\.barry\\backup\\";
92 m_filename += m_pin.Str();
93 m_filename += "\\config";
96 void ConfigFile::BuildDefaultPath()
98 TCHAR dirName[MAX_PATH];
99 CHAR dirNameA[MAX_PATH];
100 if( !SHGetSpecialFolderPath(NULL, dirName, CSIDL_APPDATA, TRUE) ) {
101 throw ConfigFileError(_("BuildDefaultPath: SHGetSpecialFolderPath failed"), GetLastError());
103 if( WideCharToMultiByte(CP_ACP, 0, dirName, -1, dirNameA, MAX_PATH, NULL, NULL) <= 0 ) {
104 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
106 dirNameA[MAX_PATH-1] = 0; // Make sure it's NUL terminated
107 m_path = dirNameA;
108 m_path += "\\.barry\\backup\\";
109 m_path += m_pin.Str();
112 /// Checks that the path in path exists, and if not, creates it.
113 /// Returns false if unable to create path, true if ok.
114 bool ConfigFile::CheckPath(const std::string &path, std::string *perr)
116 if( path.size() == 0 ) {
117 if( perr )
118 *perr = _("ConfigFile::CheckPath(): path is empty!");
119 return false;
122 TCHAR dirName[MAX_PATH];
123 if( MultiByteToWideChar(CP_ACP, 0, path.c_str(), -1, dirName, MAX_PATH) <= 0 ) {
124 if( perr )
125 *perr = _("Failed to convert to widechar");
126 return false;
128 dirName[MAX_PATH-1] = 0; // Make sure it's NUL terminated
130 DWORD attrs = GetFileAttributes(dirName);
131 if( (attrs != INVALID_ATTRIB) &&
132 ((attrs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) )
133 return true;
136 for( int i = 0; dirName[i] != 0; ++i ) {
137 if( dirName[i] != '\\' )
138 continue;
139 // At a path separator, turn it into NUL so
140 // that string handling APIs see just this part of the path
141 dirName[i] = 0;
142 DWORD attrs = GetFileAttributes(dirName);
143 if( (attrs != INVALID_ATTRIB) &&
144 ((attrs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) )
145 continue;
146 if( !CreateDirectory(dirName, NULL) ) {
147 if( perr ) {
148 *perr = _("failed to create directory");
150 return false;
152 // Turn it back into a directory separator
153 dirName[i] = '\\';
155 return true;
158 void GlobalConfigFile::BuildFilename()
160 TCHAR dirName[MAX_PATH];
161 CHAR dirNameA[MAX_PATH];
162 if (!SHGetSpecialFolderPath(NULL, dirName, CSIDL_APPDATA, TRUE)) {
163 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
165 if (WideCharToMultiByte(CP_ACP, 0, dirName, -1, dirNameA, MAX_PATH, NULL, NULL) <= 0) {
166 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
168 dirNameA[MAX_PATH-1] = 0; // Make sure it's NUL terminated
169 m_filename = dirNameA;
170 m_filename += "\\.barry\\config";
172 // build the global path too, since this never changes
173 m_path = dirNameA;
174 m_path += "\\.barry";
177 } // namespace Barry