1 /* vim:tabstop=4:expandtab:shiftwidth=4
2  *
3  * Idesk -- DesktopConfig.cpp
4  *
5  * Copyright (c) 2002, Chris (nikon) (nikon@sc.rr.com)
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  *      Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *
14  *      Redistributions in binary form must reproduce the above copyright
15  *      notice, this list of conditions and the following disclaimer in the
16  *      documentation and/or other materials provided with the distribution.
17  *
18  *      Neither the name of the <ORGANIZATION> nor the names of its
19  *      contributors may be used to endorse or promote products derived from
20  *      this software without specific prior written permission.
21  *
22  * (See the included file COPYING / BSD )
23  */
24 
25 #include "DesktopConfig.h"
26 #include "Util.h"
27 #include "sys/stat.h"
28 
29 //the initilizer list just sets the program defaults for non-necessary options
DesktopConfig(Database db,string ideskrcFile)30 DesktopConfig::DesktopConfig(Database db, string ideskrcFile) :
31                              AbstractConfig(ideskrcFile)
32 {
33     common = new CommonOptions();
34 
35     Table table = db.Query("Config");
36 
37     if ( !table.isValid())
38     {
39         cout << "Can't find config file or missing 'Config'"
40              << " table in the config file.\n";
41         _exit(1);
42     }
43 
44     //handle the configure options
45     setOptions(table);
46 
47     //delete table;  //CommonOptions object will delete the table
48     loadIcons();
49 }
50 
~DesktopConfig()51 DesktopConfig::~DesktopConfig()
52 {
53     delete common;
54 }
55 
setOptions(Table table)56 void DesktopConfig::setOptions(Table table)
57 {
58     setDefaults();
59 
60     //DesktopContainer only options
61     setDesktopOnlyOptions(table);
62 
63     //Options that can be overridden in the icon config
64     common->setCommonDefaults();
65     common->setOptions(table);
66 
67     // TODO - add functionality for other protected members in DesktopConfig.h
68 }
69 
setDefaults()70 void DesktopConfig::setDefaults()
71 {
72     isLocked = false;
73     snapOn = false;
74     snapWidth = 1;
75     snapHeight = 1;
76     startSnapTop = startSnapLeft = true;
77 
78     fileBackground = "None";
79     colorBackground = "None";
80     sourceBackground = "None";
81     delayBackground = 0; //Default Inactive
82     modeBackground = "STRETCH";
83 
84     transparency = 0;
85 
86     fontNameTip = "Arial";
87     fontSizeTip = 10;
88     foreColorTip = "#000000";
89     backColorTip = "#FFFACD";
90     captionTipOnHover = false;
91     captionTipPlacement = "Bottom";
92 }
93 
setDesktopOnlyOptions(Table table)94 void DesktopConfig::setDesktopOnlyOptions(Table table)
95 {
96     string tmpStr;
97 
98     //locking
99     if (getUpper(table.Query("Locked")) == "TRUE")
100         isLocked = true;
101     else if (getUpper(table.Query("Locked")) == "FALSE")
102         isLocked = false;
103 
104     //snap options
105     if (getUpper(table.Query("IconSnap")) == "TRUE")
106         snapOn = true;
107     else if (getUpper(table.Query("IconSnap")) == "FALSE")
108         snapOn = false;
109 
110     snapWidth = atoi(table.Query("SnapWidth").c_str());
111     snapHeight = atoi(table.Query("SnapHeight").c_str());
112 
113     tmpStr = getUpper(table.Query("SnapOrigin"));
114 
115     if (tmpStr == "BOTTOMRIGHT")
116     {
117         startSnapLeft = false;
118         startSnapTop = false;
119     }
120     else if (tmpStr == "BOTTOMLEFT")
121         startSnapTop = false;
122     else if (tmpStr == "TOPRIGHT")
123         startSnapLeft = false;
124     // last case automatically handled with default of TOPLEFT
125 
126      //File Background
127       if (table.Query("Background.File") != "")
128 	    fileBackground = table.Query("Background.File");
129 
130       if(fileBackground != ""){
131 	      struct stat b;
132 	      string directory(getenv("HOME"));
133 	      char * s = (char *)fileBackground.c_str();
134 	      directory = s[0]=='~' ? directory + &s[1]:s;
135 	      if( stat( directory.c_str(), &b ) < 0 ){
136 		      fileBackground = "None";
137 		      cerr << "[idesk] Background's file not found." << endl;
138 	      }else{
139 		      fileBackground = directory;
140 	      }
141       }
142 
143        //Color Background
144       if (table.Query("Background.Color") != "")
145 	    colorBackground = table.Query("Background.Color");
146 
147       //Source Background
148       if (table.Query("Background.Source") != "")
149 	      sourceBackground = table.Query("Background.Source");
150 
151       if(sourceBackground != ""){
152 	      struct stat b;
153 	      string directory(getenv("HOME"));
154 	      char * s = (char *)sourceBackground.c_str();
155 	      directory = s[0]=='~' ? directory + &s[1]:s;
156 	      if( stat( directory.c_str(), &b ) < 0 ) {
157 		      sourceBackground ="None";
158 		      cerr << "[idesk] Background's source not found." << endl;
159 	      }else{
160 		      sourceBackground = directory;
161 	      }
162       }
163 
164       //Delay Background
165       if (table.Query("Background.Delay") != "")
166 	      delayBackground  = atoi(table.Query("Background.Delay").c_str());
167       //Max 1 year
168       if(delayBackground > 525600)
169 	      delayBackground = 525600;
170       //Min 0 minute with this value should inactive
171       if(delayBackground < 0)
172 	      delayBackground = 0;
173 
174       //Mode Background
175       if (getUpper(table.Query("Background.Mode")) == "SCALE")
176 	      modeBackground = getUpper(table.Query("Background.Mode"));
177       if (getUpper(table.Query("Background.Mode")) == "MIRROR")
178 	      modeBackground = getUpper(table.Query("Background.Mode"));
179       if (getUpper(table.Query("Background.Mode")) == "FIT")
180 	      modeBackground = getUpper(table.Query("Background.Mode"));
181       if (getUpper(table.Query("Background.Mode")) == "CENTER")
182 	      modeBackground = getUpper(table.Query("Background.Mode"));
183       if (getUpper(table.Query("Background.Mode")) == "STRETCH")
184 	      modeBackground = getUpper(table.Query("Background.Mode"));
185 
186       //transparency
187       if (table.Query("Transparency") != "")
188         transparency = atoi(table.Query("Transparency").c_str());
189 
190       //font options for tooltip
191       if (table.Query("ToolTip.FontName") != "")
192 	    fontNameTip = table.Query("ToolTip.FontName");
193 
194       if (table.Query("ToolTip.FontSize") != "")
195 	    fontSizeTip = atoi(table.Query("ToolTip.FontSize").c_str());
196       if (fontSizeTip>256)
197               fontSizeTip = 16;
198 
199       if (table.Query("ToolTip.ForeColor") != "")
200 	    foreColorTip = table.Query("ToolTip.ForeColor");
201       if (table.Query("ToolTip.BackColor") != "")
202 	    backColorTip = table.Query("ToolTip.BackColor");
203 
204       //captionTipOnHover
205       if (getUpper(table.Query("ToolTip.CaptionOnHover")) == "TRUE")
206         captionTipOnHover = true;
207       else if (getUpper(table.Query("ToolTip.CaptionOnHover")) == "FALSE")
208         captionTipOnHover = false;
209 
210      //captionTipPlacement
211      if (getUpper(table.Query("ToolTip.CaptionPlacement")) == "BOTTOM")
212 	    captionTipPlacement = "Bottom";
213      else if (getUpper(table.Query("ToolTip.CaptionPlacement")) == "TOP")
214 	    captionTipPlacement = "Top";
215      else if (getUpper(table.Query("ToolTip.CaptionPlacement")) == "LEFT")
216 	    captionTipPlacement = "Left";
217      else if (getUpper(table.Query("ToolTip.CaptionPlacement")) == "RIGHT")
218 	    captionTipPlacement = "Right";
219      else if (getUpper(table.Query("ToolTip.CaptionPlacement")) == "AUTO")
220             captionTipPlacement = "Auto";
221 }
222 
223 
loadIcons()224 void DesktopConfig::loadIcons()
225 {
226     struct dirent **files;
227 
228     string directory(getenv("HOME"));
229     string filename;
230 
231     Database db;
232     Table table;
233 
234     int fileCount;
235 
236     DesktopIconConfig * iconPtr;
237 
238     directory += "/.idesktop/";
239 
240     fileCount = scandir(directory.c_str(), &files, 0, alphasort);
241 
242     if (fileCount == -1)
243     {
244         cout << "Error: you have to create the .idesktop dir on your HOME!!\n";
245         _exit (1);
246     }
247 
248     for(int i = 0; i < fileCount; i++)
249     {
250         if ( !backgroundFile(files[i]->d_name))
251         {
252             filename = directory + files[i]->d_name;
253 
254             db = Database(filename);
255             table = db.Query("Icon");
256 
257             if (table.isValid())
258             {
259                 iconPtr = new DesktopIconConfig(filename, common);
260                 iconConfigList.push_back(iconPtr);
261             }
262 
263             free(files[i]);
264         }
265     }
266     free(files);
267 }
268 
loadDefaultIcons()269 void DesktopConfig::loadDefaultIcons()
270 {
271 	string filename = string(DEFAULT_PREFIX) + "/share/" + string(PACKAGE) + "/default.lnk";
272 	string ideskicon = getenv("HOME");
273 	ideskicon += "/.idesktop/default.lnk";
274 	Util::copy(filename,ideskicon);
275 
276 	string iconname = string(DEFAULT_PREFIX) + "/share/" + string(PACKAGE) + "/folder_home.png";
277 
278 	DesktopIconConfig * iconPtr = new DesktopIconConfig(ideskicon, common);
279  	iconConfigList.push_back(iconPtr);
280 }
281 
saveLockState(bool lockState)282 void DesktopConfig::saveLockState(bool lockState)
283 {
284     Database db = Database(ideskrcFile);
285     Table & table = db.Query("Config");
286 
287     if(table.isValid())
288     {
289         if (lockState)
290             table.Set("Locked", "true");
291         else
292             table.Set("Locked", "false");
293 
294         db.Write();
295     }
296     else
297     {
298         cout << "Incorrect config file\n";
299         return;
300     }
301 
302 }
303 
backgroundFile(const string & filename)304 bool DesktopConfig::backgroundFile(const string & filename)
305 {
306     bool returnBool = false;
307 
308     if (filename.size() > 0 && (
309             filename[0] == '.' ||
310 	filename[filename.size() - 1] == '~' ))
311         returnBool = true;
312     else if (filename.size() > 4 && filename.substr(filename.size()-4,filename.size()) != ".lnk")
313 	    returnBool = true;
314 
315     return returnBool;
316 }
317 
318