d7cc549505f9a001b7ef4adb6136d2d5890c3936
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Profile / Ringtone.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 "Ringtone.h"
17 #include <vconf-keys.h>
18 #include <Filesystem/Path.h>
19
20 namespace {
21 enum
22 {
23     PLATFORM_VOLUME_LEVEL0 = 0,
24     PLATFORM_VOLUME_LEVEL1,
25     PLATFORM_VOLUME_LEVEL2,
26     PLATFORM_VOLUME_LEVEL3,
27     PLATFORM_VOLUME_LEVEL4,
28     PLATFORM_VOLUME_LEVEL5,
29     PLATFORM_VOLUME_LEVEL6,
30     PLATFORM_VOLUME_LEVEL7
31 };
32 } // anonymous
33
34 namespace WrtDeviceApis {
35 namespace Profile {
36 Profile::Api::Volume Ringtone::getVolume() const
37 {
38     double step = Profile::Api::VOLUME_MAX /
39         static_cast<double>(PLATFORM_VOLUME_LEVEL7);
40     Haptics::Api::Feedback level = Profile::Api::NO_VOLUME +
41         (step * m_volumeKey.getInt());
42     if (level > Profile::Api::VOLUME_MAX) {
43         level = Profile::Api::VOLUME_MAX;
44     }
45     return level;
46 }
47
48 void Ringtone::setVolume(Profile::Api::Volume level)
49 {
50     if (level > Profile::Api::VOLUME_MAX) {
51         level = Profile::Api::VOLUME_MAX;
52     }
53
54     double step = Profile::Api::VOLUME_MAX /
55         static_cast<double>(PLATFORM_VOLUME_LEVEL7);
56     for (int i = PLATFORM_VOLUME_LEVEL0; i <= PLATFORM_VOLUME_LEVEL7; ++i) {
57         if (level <= (i * step)) {
58             m_volumeKey.setValue(i);
59             break;
60         }
61     }
62 }
63
64 Filesystem::Api::IPathPtr Ringtone::getAudio() const
65 {
66     // TODO: probably not a full should be returned (maybe restricted to /mnt/ums)
67     return Filesystem::Api::IPath::create(m_audioKey.getString());
68 }
69
70 void Ringtone::setAudio(const Filesystem::Api::IPathPtr& path)
71 {
72     // TODO: it should probably be converted some way
73     m_audioKey.setValue(path->getFullPath());
74 }
75
76 Haptics::Api::Feedback Ringtone::getHaptics() const
77 {
78     double step = Haptics::Api::FEEDBACK_MAX /
79         static_cast<double>(SETTING_VIB_FEEDBACK_LEVEL5);
80     Haptics::Api::Feedback level = Haptics::Api::NO_FEEDBACK +
81         (step * m_hapticsKey.getInt());
82     if (level > Haptics::Api::FEEDBACK_MAX) {
83         level = Haptics::Api::FEEDBACK_MAX;
84     }
85     return level;
86 }
87
88 void Ringtone::setHaptics(Haptics::Api::Feedback level)
89 {
90     if (level > Haptics::Api::FEEDBACK_MAX) {
91         level = Haptics::Api::FEEDBACK_MAX;
92     }
93
94     double step = Haptics::Api::FEEDBACK_MAX /
95         static_cast<double>(SETTING_VIB_FEEDBACK_LEVEL5);
96     for (int i = SETTING_VIB_FEEDBACK_LEVEL0;
97          i <= SETTING_VIB_FEEDBACK_LEVEL5;
98          ++i) {
99         if (level <= (i * step)) {
100             m_hapticsKey.setValue(i);
101             break;
102         }
103     }
104 }
105
106 Ringtone::Ringtone(Profile::Api::RingtoneScope scope) :
107     m_scope(scope)
108 {
109     if (Profile::Api::RS_CALL == m_scope) {
110         m_volumeKey.reset(VCONFKEY_SETAPPL_PROFILE_CURRENT_CALL_VOLUME_INT);
111         m_hapticsKey.reset(VCONFKEY_SETAPPL_PROFILE_CURRENT_CALL_ALERT_VIB_INT);
112         m_audioKey.reset(VCONFKEY_SETAPPL_PROFILE_CURRENT_CALL_TONE_PATH_STR);
113     } else {
114         m_volumeKey.reset(VCONFKEY_SETAPPL_PROFILE_CURRENT_MSG_VOLUME_INT);
115         m_hapticsKey.reset(VCONFKEY_SETAPPL_PROFILE_CURRENT_MSG_ALERT_VIB_INT);
116         m_audioKey.reset(VCONFKEY_SETAPPL_PROFILE_CURRENT_MSG_TONE_PATH_STR);
117     }
118 }
119 } // Profile
120 } // WrtDeviceApis