Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / registry.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Registry Utils
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/utils/file.h>
21
22 #include <freerdp/utils/registry.h>
23
24 static const char registry_dir[] = "freerdp";
25 static const char registry_file[] = "config.txt";
26
27 static REG_SECTION global[] =
28 {
29         { REG_TYPE_SECTION, "global",                   0,  NULL },
30         { REG_TYPE_BOOLEAN, "fast_path",                1,  "1" },
31         { REG_TYPE_STRING,  "resolution",               8,  "1024x768" },
32         { REG_TYPE_INTEGER, "performance_flags",        4,  "0xFFFF" },
33         { REG_TYPE_NONE,    "",                         0,  NULL }
34 };
35
36 static REG_SECTION licensing[] =
37 {
38         { REG_TYPE_SECTION, "licensing",        0,  NULL },
39         { REG_TYPE_STRING,  "platform_id",      1,  "0x000201" },
40         { REG_TYPE_STRING,  "hardware_id",      16, "0xe107d9d372bb6826bd81d3542a419d6" },
41         { REG_TYPE_NONE,    "",                 0,  NULL }
42 };
43
44 static REG_SECTION* sections[] =
45 {
46         (REG_SECTION*) &global,
47         (REG_SECTION*) &licensing,
48         (REG_SECTION*) NULL
49 };
50
51 void registry_print_entry(REG_ENTRY* entry, FILE* fp)
52 {
53         uint8* value;
54         value = (uint8*) entry->value;
55         fprintf(fp, "%s = %s\n", entry->name, value);
56 }
57
58 void registry_print_section(REG_SECTION* section, FILE* fp)
59 {
60         int i = 0;
61         REG_ENTRY* entries = (REG_ENTRY*) &section[1];
62
63         fprintf(fp, "\n");
64         fprintf(fp, "[%s]\n", section->name);
65
66         while (entries[i].type != REG_TYPE_NONE)
67         {
68                 registry_print_entry(&entries[i], fp);
69                 i++;
70         }
71 }
72
73 void registry_print(rdpRegistry* registry, FILE* fp)
74 {
75         int i = 0;
76
77         fprintf(fp, "# FreeRDP Configuration Registry\n");
78
79         while (sections[i] != NULL)
80         {
81                 registry_print_section(sections[i], fp);
82                 i++;
83         }
84
85         fprintf(fp, "\n");
86 }
87
88 void registry_create(rdpRegistry* registry)
89 {
90         registry->fp = fopen((char*)registry->file, "w+");
91
92         if (registry->fp == NULL)
93         {
94                 printf("registry_create: error opening [%s] for writing\n", registry->file);
95                 return;
96         }
97
98         registry_print(registry, registry->fp);
99         fflush(registry->fp);
100 }
101
102 void registry_load(rdpRegistry* registry)
103 {
104         registry->fp = fopen((char*) registry->file, "r+");
105 }
106
107 void registry_open(rdpRegistry* registry)
108 {
109         struct stat stat_info;
110
111         if (stat((char*)registry->file, &stat_info) != 0)
112                 registry_create(registry);
113         else
114                 registry_load(registry);
115 }
116
117 void registry_close(rdpRegistry* registry)
118 {
119         if (registry->fp != NULL)
120                 fclose(registry->fp);
121 }
122
123 void registry_init(rdpRegistry* registry)
124 {
125         int length;
126         char* home_path;
127         struct stat stat_info;
128
129         if (registry->settings->home_path == NULL)
130                 home_path = getenv("HOME");
131         else
132                 home_path = registry->settings->home_path;
133
134         if (home_path == NULL)
135         {
136                 printf("could not get home path\n");
137                 registry->available = false;
138                 return;
139         }
140
141         registry->available = true;
142
143         if (home_path == NULL)
144         {
145                 printf("could not get home path\n");
146                 registry->available = false;
147                 return;
148         }
149
150         registry->home = (char*) xstrdup(home_path);
151         printf("home path: %s\n", registry->home);
152
153         registry->path = (char*) xmalloc(strlen(registry->home) + strlen("/.") + strlen(registry_dir) + 1);
154         sprintf(registry->path, "%s/.%s", registry->home, registry_dir);
155         printf("registry path: %s\n", registry->path);
156
157         if (stat(registry->path, &stat_info) != 0)
158         {
159                 freerdp_mkdir(registry->path);
160                 printf("creating directory %s\n", registry->path);
161         }
162
163         length = strlen(registry->path);
164         registry->file = (char*) xmalloc(strlen(registry->path) + strlen("/") + strlen(registry_file) + 1);
165         sprintf(registry->file, "%s/%s", registry->path, registry_file);
166         printf("registry file: %s\n", registry->file);
167
168         registry_open(registry);
169 }
170
171 rdpRegistry* registry_new(rdpSettings* settings)
172 {
173         rdpRegistry* registry = (rdpRegistry*) xzalloc(sizeof(rdpRegistry));
174
175         if (registry != NULL)
176         {
177                 registry->settings = settings;
178                 registry_init(registry);
179         }
180
181         return registry;
182 }
183
184 void registry_free(rdpRegistry* registry)
185 {
186         if (registry != NULL)
187         {
188                 registry_close(registry);
189                 xfree(registry->path);
190                 xfree(registry->file);
191                 xfree(registry->home);
192                 xfree(registry);
193         }
194 }