menu: added new Keywords tag to .desktop files
[barry.git] / src / configfilewin32.cc
blob94bd8c54be7ed995b1485a68238aa82a8b74109b
1 ///
2 /// \file configfilewin32.cc
3 /// Barry configuration class, for one device PIN, using Win32 APIs
4 ///
6 /*
7 Copyright (C) 2007-2013, 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 "i18n.h"
24 #include "configfile.h"
25 #include "error.h"
26 #include "r_message.h"
27 #include <iostream>
28 #include <iomanip>
29 #include <sstream>
30 #include <string.h>
31 #include <Shlobj.h>
32 #include <winbase.h>
34 // Returned by GetFileAttributes to indicate failure
35 #define INVALID_ATTRIB 0xFFFFFFFF
37 namespace Barry {
40 /// Creates a tar.gz filename using PIN + date + time + label.
41 /// Does not include any path, just returns a new filename.
42 std::string MakeBackupFilename(const Barry::Pin &pin,
43 const std::string &label)
45 using namespace std;
47 SYSTEMTIME st;
48 GetLocalTime(&st);
50 std::string fileLabel = label;
51 if( fileLabel.size() ) {
52 // prepend a hyphen
53 fileLabel.insert(fileLabel.begin(), '-');
55 // translate all spaces and slashes
56 for( size_t i = 0; i < fileLabel.size(); i++ ) {
57 if( fileLabel[i] == ' ' )
58 fileLabel[i] = '_';
59 else if( fileLabel[i] == '/' )
60 fileLabel[i] = '-';
61 else if( fileLabel[i] == '\\' )
62 fileLabel[i] = '-';
66 ostringstream tarfilename;
67 tarfilename << pin.Str() << "-"
68 << setw(4) << setfill('0') << st.wYear
69 << setw(2) << setfill('0') << st.wMonth
70 << setw(2) << setfill('0') << st.wDay
71 << "-"
72 << setw(2) << setfill('0') << st.wHour
73 << setw(2) << setfill('0') << st.wMinute
74 << setw(2) << setfill('0') << st.wSecond
75 << fileLabel
76 << ".tar.gz";
77 return tarfilename.str();
80 void ConfigFile::BuildFilename()
82 TCHAR dirName[MAX_PATH];
83 CHAR dirNameA[MAX_PATH];
84 if( !SHGetSpecialFolderPath(NULL, dirName, CSIDL_APPDATA, TRUE) ) {
85 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
87 if( WideCharToMultiByte(CP_ACP, 0, dirName, -1, dirNameA, MAX_PATH, NULL, NULL) <= 0 ) {
88 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
90 dirNameA[MAX_PATH-1] = 0; // Make sure it's NUL terminated
91 m_filename = dirNameA;
92 m_filename += "\\.barry\\backup\\";
93 m_filename += m_pin.Str();
94 m_filename += "\\config";
97 void ConfigFile::BuildDefaultPath()
99 TCHAR dirName[MAX_PATH];
100 CHAR dirNameA[MAX_PATH];
101 if( !SHGetSpecialFolderPath(NULL, dirName, CSIDL_APPDATA, TRUE) ) {
102 throw ConfigFileError(_("BuildDefaultPath: SHGetSpecialFolderPath failed"), GetLastError());
104 if( WideCharToMultiByte(CP_ACP, 0, dirName, -1, dirNameA, MAX_PATH, NULL, NULL) <= 0 ) {
105 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
107 dirNameA[MAX_PATH-1] = 0; // Make sure it's NUL terminated
108 m_path = dirNameA;
109 m_path += "\\.barry\\backup\\";
110 m_path += m_pin.Str();
113 /// Checks that the path in path exists, and if not, creates it.
114 /// Returns false if unable to create path, true if ok.
115 bool ConfigFile::CheckPath(const std::string &path, std::string *perr)
117 if( path.size() == 0 ) {
118 if( perr )
119 *perr = _("ConfigFile::CheckPath(): path is empty!");
120 return false;
123 TCHAR dirName[MAX_PATH];
124 if( MultiByteToWideChar(CP_ACP, 0, path.c_str(), -1, dirName, MAX_PATH) <= 0 ) {
125 if( perr )
126 *perr = _("Failed to convert to widechar");
127 return false;
129 dirName[MAX_PATH-1] = 0; // Make sure it's NUL terminated
131 DWORD attrs = GetFileAttributes(dirName);
132 if( (attrs != INVALID_ATTRIB) &&
133 ((attrs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) )
134 return true;
137 for( int i = 0; dirName[i] != 0; ++i ) {
138 if( dirName[i] != '\\' )
139 continue;
140 // At a path separator, turn it into NUL so
141 // that string handling APIs see just this part of the path
142 dirName[i] = 0;
143 DWORD attrs = GetFileAttributes(dirName);
144 if( (attrs != INVALID_ATTRIB) &&
145 ((attrs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) )
146 continue;
147 if( !CreateDirectory(dirName, NULL) ) {
148 if( perr ) {
149 *perr = _("failed to create directory");
151 return false;
153 // Turn it back into a directory separator
154 dirName[i] = '\\';
156 return true;
159 void GlobalConfigFile::BuildFilename()
161 TCHAR dirName[MAX_PATH];
162 CHAR dirNameA[MAX_PATH];
163 if (!SHGetSpecialFolderPath(NULL, dirName, CSIDL_APPDATA, TRUE)) {
164 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
166 if (WideCharToMultiByte(CP_ACP, 0, dirName, -1, dirNameA, MAX_PATH, NULL, NULL) <= 0) {
167 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
169 dirNameA[MAX_PATH-1] = 0; // Make sure it's NUL terminated
170 m_filename = dirNameA;
171 m_filename += "\\.barry\\config";
173 // build the global path too, since this never changes
174 m_path = dirNameA;
175 m_path += "\\.barry";
178 } // namespace Barry