1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 
27 #include "config.hxx"
28 #include "myconfigurationhelper.hxx"
29 #include <rtl/ustrbuf.hxx>
30 
31 
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::uno;
34 using ::rtl::OUString;
35 using ::rtl::OUStringBuffer;
36 
37 
38 namespace
39 {
40     using namespace oooimprovement;
41 
42     static const OUString CFG_COUNTERS = OUString::createFromAscii("Counters");
43     static const OUString CFG_ENABLINGALLOWED = OUString::createFromAscii("EnablingAllowed");
44     static const OUString CFG_EVENTSCOUNT = OUString::createFromAscii("LoggedEvents");
45     static const OUString CFG_EXTENSION = OUString::createFromAscii("ooSetupExtension");
46     static const OUString CFG_FAILEDATTEMPTS = OUString::createFromAscii("FailedAttempts");
47     static const OUString CFG_INVACCEPT = OUString::createFromAscii("InvitationAccepted");
48     static const OUString CFG_L10N = OUString::createFromAscii("L10N");
49     static const OUString CFG_LOCALE = OUString::createFromAscii("ooLocale");
50     static const OUString CFG_LOGGING = OUString::createFromAscii("/org.openoffice.Office.Logging");
51     static const OUString CFG_LOGPATH = OUString::createFromAscii("LogPath");
52     static const OUString CFG_NAME = OUString::createFromAscii("ooName");
53     static const OUString CFG_OFFICESTARTCOUNTDOWN = OUString::createFromAscii("OfficeStartCounterdown");
54     static const OUString CFG_OOOIMPROVEMENT = OUString::createFromAscii("OOoImprovement");
55     static const OUString CFG_OOOIMPROVEMENTPACK = OUString::createFromAscii("/org.openoffice.Office.OOoImprovement.Settings");
56     static const OUString CFG_PARTICIPATION = OUString::createFromAscii("Participation");
57     static const OUString CFG_PRODUCT = OUString::createFromAscii("Product");
58     static const OUString CFG_REPORTCOUNT = OUString::createFromAscii("UploadedReports");
59     static const OUString CFG_REPORTEREMAIL = OUString::createFromAscii("ReporterEmail");
60     static const OUString CFG_SETUP = OUString::createFromAscii("/org.openoffice.Setup");
61     static const OUString CFG_SHOWEDINV = OUString::createFromAscii("ShowedInvitation");
62     static const OUString CFG_SOAPIDADD = OUString::createFromAscii("SoapIdAdditions");
63     static const OUString CFG_SOAPURL = OUString::createFromAscii("SoapUrl");
64     static const OUString CFG_UPLOAD = OUString::createFromAscii("Upload");
65     static const OUString CFG_VERSION = OUString::createFromAscii("ooSetupVersion");
66 
67     static const OUString SOAPID = OUString::createFromAscii("OpenOffice.org Improvement Report - Version 1\n");
68 
incrementCfgValue(const Reference<XMultiServiceFactory> sm,const OUString & package,const OUString & rel_path,const OUString & key,sal_Int32 increment_by)69     static sal_Int32 incrementCfgValue(
70         const Reference<XMultiServiceFactory> sm,
71         const OUString& package,
72         const OUString& rel_path,
73         const OUString& key,
74         sal_Int32 increment_by)
75     {
76         sal_Int32 value;
77         Reference<XInterface> cfg =
78             MyConfigurationHelper::openConfig(
79                 sm,
80                 package, MyConfigurationHelper::E_STANDARD);
81         MyConfigurationHelper::readRelativeKey(
82             cfg,
83             rel_path, key) >>= value;
84         value += increment_by;
85         MyConfigurationHelper::writeRelativeKey(
86             cfg,
87             rel_path, key,
88             Any(value));
89         MyConfigurationHelper::flush(cfg);
90         return value;
91     };
92 }
93 
94 namespace oooimprovement
95 {
Config(const Reference<XMultiServiceFactory> & sf)96     Config::Config(const Reference<XMultiServiceFactory>& sf)
97         : m_ServiceFactory(sf)
98     {}
99 
getSoapUrl() const100     OUString Config::getSoapUrl() const
101     {
102         OUString result;
103         MyConfigurationHelper::readDirectKey(
104             m_ServiceFactory,
105             CFG_OOOIMPROVEMENTPACK, CFG_UPLOAD, CFG_SOAPURL,
106             MyConfigurationHelper::E_READONLY) >>= result;
107         return result;
108     }
109 
getSoapId() const110     OUString Config::getSoapId() const
111     {
112         OUString value;
113         OUStringBuffer result = SOAPID;
114         MyConfigurationHelper::readDirectKey(
115             m_ServiceFactory,
116             CFG_OOOIMPROVEMENTPACK, CFG_UPLOAD, CFG_SOAPIDADD,
117             MyConfigurationHelper::E_READONLY) >>= value;
118         result.append(value);
119         return result.makeStringAndClear();
120     }
121 
getReporterEmail() const122     OUString Config::getReporterEmail() const
123     {
124         OUString result;
125         MyConfigurationHelper::readDirectKey(
126             m_ServiceFactory,
127             CFG_OOOIMPROVEMENTPACK, CFG_UPLOAD, CFG_REPORTEREMAIL,
128             MyConfigurationHelper::E_READONLY) >>= result;
129         return result;
130     }
131 
getLogPath() const132     OUString Config::getLogPath() const
133     {
134         OUString result;
135         MyConfigurationHelper::readDirectKey(
136             m_ServiceFactory,
137             CFG_LOGGING, CFG_OOOIMPROVEMENT, CFG_LOGPATH,
138             MyConfigurationHelper::E_READONLY) >>= result;
139         return result;
140     }
141 
getEnablingAllowed() const142     bool Config::getEnablingAllowed() const
143     {
144         bool result = false;
145         MyConfigurationHelper::readDirectKey(
146             m_ServiceFactory,
147             CFG_LOGGING, CFG_OOOIMPROVEMENT, CFG_ENABLINGALLOWED,
148             MyConfigurationHelper::E_READONLY) >>= result;
149         return result;
150     }
151 
getInvitationAccepted() const152     bool Config::getInvitationAccepted() const
153     {
154        bool result = false;
155        MyConfigurationHelper::readDirectKey(
156             m_ServiceFactory,
157             CFG_OOOIMPROVEMENTPACK, CFG_PARTICIPATION, CFG_INVACCEPT,
158             MyConfigurationHelper::E_READONLY) >>= result;
159        return result;
160     };
161 
getShowedInvitation() const162     bool Config::getShowedInvitation() const
163     {
164        bool result = false;
165        MyConfigurationHelper::readDirectKey(
166             m_ServiceFactory,
167             CFG_OOOIMPROVEMENTPACK, CFG_PARTICIPATION, CFG_SHOWEDINV,
168             MyConfigurationHelper::E_READONLY) >>= result;
169        return result;
170     };
171 
getCompleteProductname() const172     OUString Config::getCompleteProductname() const
173     {
174         OUStringBuffer result;
175         OUString value;
176         MyConfigurationHelper::readDirectKey(
177             m_ServiceFactory,
178             CFG_SETUP, CFG_PRODUCT, CFG_NAME,
179             MyConfigurationHelper::E_READONLY) >>= value;
180         result.append(value);
181 
182         value = OUString::createFromAscii("");
183         MyConfigurationHelper::readDirectKey(
184             m_ServiceFactory,
185             CFG_SETUP, CFG_PRODUCT, CFG_VERSION,
186             MyConfigurationHelper::E_READONLY) >>= value;
187         if(value.getLength()) result.appendAscii(" ").append(value);
188 
189         value = OUString::createFromAscii("");
190         MyConfigurationHelper::readDirectKey(
191             m_ServiceFactory,
192             CFG_SETUP, CFG_PRODUCT, CFG_EXTENSION,
193             MyConfigurationHelper::E_READONLY) >>= value;
194         if(value.getLength()) result.appendAscii(" ").append(value);
195 
196         return result.makeStringAndClear();
197     }
198 
getSetupLocale() const199     OUString Config::getSetupLocale() const
200     {
201         OUString result;
202         MyConfigurationHelper::readDirectKey(
203             m_ServiceFactory,
204             CFG_SETUP, CFG_L10N, CFG_LOCALE,
205             MyConfigurationHelper::E_READONLY) >>= result;
206         return result;
207     }
208 
getReportCount() const209     sal_Int32 Config::getReportCount() const
210     {
211         sal_Int32 result = 0;
212         MyConfigurationHelper::readDirectKey(
213             m_ServiceFactory,
214             CFG_OOOIMPROVEMENTPACK, CFG_COUNTERS, CFG_REPORTCOUNT,
215             MyConfigurationHelper::E_READONLY) >>= result;
216         return result;
217     }
218 
219 #ifdef FUTURE
getFailedAttempts() const220     sal_Int32 Config::getFailedAttempts() const
221     {
222         sal_Int32 result = 0;
223         MyConfigurationHelper::readDirectKey(
224             m_ServiceFactory,
225             CFG_OOOIMPROVEMENTPACK, CFG_COUNTERS, CFG_FAILEDATTEMPTS,
226             MyConfigurationHelper::E_READONLY) >>= result;
227         return result;
228     }
229 #endif
230 
getOfficeStartCounterdown() const231     sal_Int32 Config::getOfficeStartCounterdown() const
232     {
233         sal_Int32 result = 0;
234         MyConfigurationHelper::readDirectKey(
235             m_ServiceFactory,
236             CFG_OOOIMPROVEMENTPACK, CFG_PARTICIPATION, CFG_OFFICESTARTCOUNTDOWN,
237             MyConfigurationHelper::E_READONLY) >>= result;
238         return result;
239     }
240 
incrementReportCount(sal_Int32 by)241     sal_Int32 Config::incrementReportCount(sal_Int32 by)
242     {
243         return incrementCfgValue(
244             m_ServiceFactory,
245             CFG_OOOIMPROVEMENTPACK, CFG_COUNTERS, CFG_REPORTCOUNT,
246             by);
247     }
248 
incrementEventCount(sal_Int32 by)249     sal_Int32 Config::incrementEventCount(sal_Int32 by)
250     {
251         return incrementCfgValue(
252             m_ServiceFactory,
253             CFG_OOOIMPROVEMENTPACK, CFG_COUNTERS, CFG_EVENTSCOUNT,
254             by);
255     }
256 
incrementFailedAttempts(sal_Int32 by)257     sal_Int32 Config::incrementFailedAttempts(sal_Int32 by)
258     {
259         return incrementCfgValue(
260             m_ServiceFactory,
261             CFG_OOOIMPROVEMENTPACK, CFG_COUNTERS, CFG_FAILEDATTEMPTS,
262             by);
263     }
264 
decrementOfficeStartCounterdown(sal_Int32 by)265     sal_Int32 Config::decrementOfficeStartCounterdown(sal_Int32 by)
266     {
267         return incrementCfgValue(
268             m_ServiceFactory,
269             CFG_OOOIMPROVEMENTPACK, CFG_PARTICIPATION, CFG_OFFICESTARTCOUNTDOWN,
270             -by);
271     }
272 
resetFailedAttempts()273     void Config::resetFailedAttempts()
274     {
275         sal_Int32 zero = 0;
276         MyConfigurationHelper::writeDirectKey(
277             m_ServiceFactory,
278             CFG_OOOIMPROVEMENTPACK, CFG_COUNTERS, CFG_FAILEDATTEMPTS,
279             Any(zero),
280             MyConfigurationHelper::E_STANDARD);
281     }
282 
giveupUploading()283     void Config::giveupUploading()
284     {
285         sal_Bool f = false;
286         MyConfigurationHelper::writeDirectKey(
287             m_ServiceFactory,
288             CFG_OOOIMPROVEMENTPACK, CFG_PARTICIPATION, CFG_INVACCEPT,
289             Any(f),
290             MyConfigurationHelper::E_STANDARD);
291         resetFailedAttempts();
292     }
293 }
294