1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010, 2013 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "sarmanager.h"
21 
22 #include <cstdio> // sprintf
23 
24 #include <licq/inifile.h>
25 
26 using namespace LicqDaemon;
27 using Licq::SarList;
28 using Licq::SavedAutoResponse;
29 
30 // Declare global SarManager (internal for daemon)
31 LicqDaemon::SarManager LicqDaemon::gSarManager;
32 
33 // Declare global Licq::SarManager to refer to the internal SarManager
34 Licq::SarManager& Licq::gSarManager(LicqDaemon::gSarManager);
35 
36 const char* const SarManager::SectionNames[SarManager::NumLists] =
37     { "Away", "NA", "Occupied", "DND", "FFC" };
38 
39 const char* const SarManager::DefaultNames[SarManager::NumLists][SarManager::NumDefaults] = {
40         { "Away", "Lunch", "Be right back", "Dog Walk", "Smoke", "Coffee", "Air" },
41         { "Out for the day", "Not here", "Closed", "Fishing", "Sleeping", "Kids", "Gone" },
42         { "Busy", "Meeting", "Concentration", "On the Phone", "Can't chat", "Working", "Conversing" },
43         { "Do Not Disturb", "Meeting", "Concentration", "On the Phone", "Working", "Fiddling"},
44         { "Chat", "Come in", "Fun", "Don't Wait", "Hear", NULL, NULL}
45     };
46 
47 const char* const SarManager::DefaultTexts[SarManager::NumLists][SarManager::NumDefaults] = {
48     {
49         "I am currently away from the keyboard.\\nPlease leave your message and I will get back to you as soon as I return!",
50         "I'm out to lunch.\\nI will return shortly.",
51         "Don't go anywhere!\\nI'll be back in a jiffy.",
52         "I'm out with the dog.\\nBe back when he's finished",
53         "Went out for a smoke.",
54         "On my coffee break.",
55         "Went to get some fresh air.",
56     }, {
57         "I am out'a here.\\nSee you tomorrow!",
58         "Give it up! I 'm not in!",
59         "I'm closed for the weekend/holidays.",
60         "Gone fishin'!",
61         "I'm sleeping. Don't wake me.",
62         "Went home.\\nHad to feed the kids.",
63         "Gone for good.",
64     }, {
65         "Please do not disturb me now.\\nDisturb me later.\\nOnly urgent messages, please!",
66         "I'm currently in a meeting. I can't be disturbed.\\nOnly urgent messages, please!",
67         "Don't disturb my concentration!\\nOnly urgent messages, please!",
68         "hone with a very important client. Don't disturb me!\\nOnly urgent messages please!",
69         "I can't chat with you now. I'm busy.\\nOnly urgent messages, please!",
70         "Can't you see I'm working?\\nOnly urgent messages, please!",
71         "I am conversing with my colleagues.\\nOnly urgent messages, please!",
72     }, {
73         "Please do not disturb me now. Disturb me later.",
74         "I'm currently in a meeting.\\nI can't be disturbed.",
75         "Don't disturb my concentration!",
76         "I'm on the phone with a very important client.\\nDon't disturb me!",
77         "I can't chat with you now. I'm busy.",
78         "Can't you see I'm working?",
79         "Fiddling around some things and I'm quite busy with that.\\nSo please disturb me later!",
80     }, {
81         "We'd love to hear what you have to say. Join our chat.",
82         "Come in and join my chat room!",
83         "Don't miss the fun! Join our chat!",
84         "What are you waiting for? Come in!",
85         "We'd love to hear\\nwhat you have to say.\\nJoin our chat!",
86         NULL,
87         NULL,
88     } };
89 
90 
SarManager()91 SarManager::SarManager()
92 {
93   // Empty
94 }
95 
~SarManager()96 SarManager::~SarManager()
97 {
98   // Empty
99 }
100 
initialize()101 void SarManager::initialize()
102 {
103   Licq::IniFile sarFile("sar.conf");
104 
105   if (sarFile.loadFile())
106   {
107     for (int i = 0; i < NumLists; ++i)
108     {
109       sarFile.setSection(SectionNames[i]);
110       int count;
111       sarFile.get("NumSAR", count, 0);
112       for (int j = 1; j <= count; ++j)
113       {
114         SavedAutoResponse sar;
115         char key[20];
116         sprintf(key, "SAR%i.Name", j);
117         sarFile.get(key, sar.name, "");
118         sprintf(key, "SAR%i.Text", j);
119         sarFile.get(key, sar.text, "");
120         if (sar.name.empty() && sar.text.empty())
121           continue;
122 
123         mySarLists[i].push_back(sar);
124       }
125     }
126 
127     return;
128   }
129 
130   // No existing config file, load up defaults
131   for (int i = 0; i < NumLists; ++i)
132   {
133     if (mySarLists[i].size() > 0)
134       continue;
135 
136     for (int j = 0; j < NumDefaults; ++j)
137     {
138       if (DefaultNames[i][j] == NULL)
139         continue;
140       SavedAutoResponse sar;
141       sar.name = DefaultNames[i][j];
142       sar.text = DefaultTexts[i][j];
143       mySarLists[i].push_back(sar);
144     }
145   }
146 
147   writeConfig();
148 }
149 
getList(List list)150 Licq::SarList& SarManager::getList(List list)
151 {
152   myMutex.lock();
153   return mySarLists[list];
154 }
155 
releaseList(bool save)156 void SarManager::releaseList(bool save)
157 {
158   if (save)
159     writeConfig();
160 
161   myMutex.unlock();
162 }
163 
writeConfig()164 void SarManager::writeConfig()
165 {
166   Licq::IniFile sarFile("sar.conf");
167   sarFile.loadFile();
168 
169   for (int i = 0; i < NumLists; ++i)
170   {
171     sarFile.setSection(SectionNames[i]);
172     sarFile.set("NumSAR", mySarLists[i].size());
173     int count = 0;
174     for (SarList::const_iterator j = mySarLists[i].begin(); j != mySarLists[i].end(); ++j)
175     {
176       ++count;
177       char key[20];
178       sprintf(key, "SAR%i.Name", count);
179       sarFile.set(key, j->name);
180       sprintf(key, "SAR%i.Text", count);
181       sarFile.set(key, j->text);
182     }
183   }
184   sarFile.writeFile();
185 }
186