c075d5438a4eb1510a5168aad522018e66cd15e1
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Profile / Manager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include "Manager.h"
17 #include <Commons/Exception.h>
18 #include <vconf-keys.h>
19 #include "Ringtone.h"
20 #include "Desktop.h"
21
22 #define __TEMP_OPTION_FOR_GET_CURRENT_VOLUME__  // We can't get current volume by VCONF. This option is temporary till VCONF is woring correctly.
23 #if defined (__TEMP_OPTION_FOR_GET_CURRENT_VOLUME__)
24 #include <mm_types.h>
25 #include <mm_player.h>
26 #include <mm_error.h>
27 #include <mm_sound.h>
28 #endif
29
30 namespace {
31 const std::size_t NUMBER_OF_AVAILABLE_DESKTOPS = 1;
32 const std::size_t DEFAULT_DESKTOP_INDEX = 1;
33
34 const WrtDeviceApis::Profile::Api::IManager::ProfileType profileMap[] = {
35     WrtDeviceApis::Profile::Api::IManager::NORMAL,
36     WrtDeviceApis::Profile::Api::IManager::SILENT,
37     WrtDeviceApis::Profile::Api::IManager::DRIVING,
38     WrtDeviceApis::Profile::Api::IManager::MEETING,
39     WrtDeviceApis::Profile::Api::IManager::OUTDOOR,
40     WrtDeviceApis::Profile::Api::IManager::FLIGHT
41 };
42 const size_t PROFILE_MAP_SIZE = sizeof(profileMap) / sizeof(profileMap[0]);
43 } // anonymous
44
45 namespace WrtDeviceApis {
46 namespace Profile {
47 Manager& Manager::getInstance()
48 {
49     static Manager instance;
50     return instance;
51 }
52
53 Manager::~Manager()
54 {
55 }
56
57 void Manager::Release() const
58 {
59     LogDebug("Entered.");
60     Ringtones::iterator it1 = m_ringtones.begin();
61     for (; it1 != m_ringtones.end(); ++it1) {
62         delete it1->second;
63     }
64     if (m_ringtones.size() > 0) {
65         m_ringtones.clear();
66     }
67
68     Desktops::iterator it2 = m_desktops.begin();
69     for (; it2 != m_desktops.end(); ++it2) {
70         if (it2->second) {
71             //delete it2->second;
72         }
73     }
74     if (m_desktops.size() > 0) {
75         m_desktops.clear();
76     }
77 }
78
79 Profile::Api::IDesktopPtr Manager::getDesktop(std::size_t index) const
80 {
81     if (index == Profile::Api::IDesktop::DEFAULT) {
82         index = DEFAULT_DESKTOP_INDEX;
83     }
84
85     if (index > NUMBER_OF_AVAILABLE_DESKTOPS) {
86         Throw(Commons::OutOfRangeException);
87     }
88
89     Desktops::const_iterator it = m_desktops.find(index);
90     if (it != m_desktops.end()) {
91         return it->second;
92     }
93     return (m_desktops[index] = Profile::Api::IDesktopPtr(new Desktop()));
94 }
95
96 std::size_t Manager::getDesktopCount() const
97 {
98     return NUMBER_OF_AVAILABLE_DESKTOPS;
99 }
100
101 Profile::Api::IRingtone* Manager::getRingtone(Profile::Api::RingtoneScope scope)
102 const
103 {
104     switch (scope) {
105     case Profile::Api::RS_CALL:
106         return getRingtone_(Profile::Api::RS_CALL);
107     case Profile::Api::RS_SMS:
108     case Profile::Api::RS_MMS:
109     case Profile::Api::RS_EMAIL:
110         return getRingtone_(Profile::Api::RS_SMS);
111     default:
112         Throw(Commons::UnsupportedException);
113     }
114 }
115
116 Manager::Manager() :
117     m_profileKey(VCONFKEY_SETAPPL_CUR_PROFILE_INT),
118     m_volumeKey(VCONFKEY_SETAPPL_PROFILE_CURRENT_CALL_VOLUME_INT),
119     m_vibrationKey(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL)
120 {
121 }
122
123 Profile::Api::IRingtone* Manager::getRingtone_(
124         Profile::Api::RingtoneScope scope) const
125 {
126     Ringtones::const_iterator it = m_ringtones.find(scope);
127     if (it != m_ringtones.end()) {
128         return it->second;
129     }
130     return (m_ringtones[scope] = new Ringtone(scope));
131 }
132
133 Profile::Api::IManager::ProfileType Manager::getProfileType() const
134 {
135     int profile = m_profileKey.getInt();
136     if (profile < 0 || profile >= static_cast<int>(PROFILE_MAP_SIZE)) {
137         Throw(Commons::OutOfRangeException);
138     }
139
140     return profileMap[profile];
141 }
142
143 int Manager::getSystemVolume() const
144 {
145 #if defined (__TEMP_OPTION_FOR_GET_CURRENT_VOLUME__)
146     unsigned int volume = 0;
147     int err = mm_sound_volume_get_value(VOLUME_TYPE_SYSTEM, &volume);
148     if (MM_ERROR_NONE != err) {
149         LogError("Can't get volume level. Error code: " << std::hex << err);
150         volume = 0;
151     }
152     return volume;
153 #else
154     return m_volumeKey.getInt();
155 #endif
156 }
157
158 int Manager::getVibrateType() const
159 {
160     return m_vibrationKey.getBool();
161 }
162 } // Profile
163 } // WrtDeviceApis