1 /*
2  * Copyright © 2009-2010 freedcpp, http://code.google.com/p/freedcpp
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * In addition, as a special exception, compiling, linking, and/or
19  * using OpenSSL with this program is allowed.
20  */
21 
22 #ifdef USE_LIBGNOME2
23     #include <libgnome/gnome-sound.h>
24 #endif
25 #ifdef USE_LIBCANBERRA
26     #include <canberra-gtk.h>
27 #endif
28 
29 #include "settingsmanager.hh"
30 #include <dcpp/Text.h>
31 #include "sound.hh"
32 #include "WulforUtil.hh"
33 
34 using namespace std;
35 using namespace dcpp;
36 
37 Sound *Sound::pSound = NULL;
38 
39 #ifdef USE_LIBCANBERRA
40 ca_context *context;
41 #endif
42 
start()43 void Sound::start()
44 {
45     dcassert(!pSound);
46     pSound = new Sound();
47 }
48 
stop()49 void Sound::stop()
50 {
51     dcassert(pSound);
52     delete pSound;
53     pSound = NULL;
54 }
55 
get()56 Sound* Sound::get()
57 {
58     dcassert(pSound);
59     return pSound;
60 }
61 
sound_init()62 void Sound::sound_init()
63 {
64 #ifdef USE_LIBGNOME2
65     gnome_sound_init(NULL);
66     dcdebug("Sound::sound_init: Esound connection %d...\n", gnome_sound_connection_get());
67 #elif USE_LIBCANBERRA
68     int res = ca_context_create(&context);
69     dcdebug("Sound::sound_init: connection %d...\n", res);
70 #endif
71 }
72 
playSound(TypeSound sound)73 void Sound::playSound(TypeSound sound)
74 {
75     WulforSettingsManager *wsm = WulforSettingsManager::getInstance();
76 
77     switch (sound)
78     {
79 //      TODO: download begins, uncomment when implemented
80 //      case DOWNLOAD_BEGINS:
81 //
82 //          if (wsm->getInt("sound-download-begins-use"))
83 //              playSound(wsm->getString("sound-download-begins"));
84 //      break;
85 
86         case DOWNLOAD_FINISHED:
87 
88             if (wsm->getInt("sound-download-finished-use"))
89                 playSound(wsm->getString("sound-download-finished"));
90         break;
91 
92         case DOWNLOAD_FINISHED_USER_LIST:
93 
94             if (wsm->getInt("sound-download-finished-ul-use"))
95                 playSound(wsm->getString("sound-download-finished-ul"));
96         break;
97 
98         case UPLOAD_FINISHED:
99 
100             if (wsm->getInt("sound-upload-finished-use"))
101                 playSound(wsm->getString("sound-upload-finished"));
102         break;
103 
104         case PRIVATE_MESSAGE:
105 
106             if (wsm->getInt("sound-private-message-use"))
107                 playSound(wsm->getString("sound-private-message"));
108         break;
109 
110         case HUB_CONNECT:
111 
112             if (wsm->getInt("sound-hub-connect-use"))
113                 playSound(wsm->getString("sound-hub-connect"));
114         break;
115 
116         case HUB_DISCONNECT:
117 
118             if (wsm->getInt("sound-hub-disconnect-use"))
119                 playSound(wsm->getString("sound-hub-disconnect"));
120         break;
121 
122         case FAVORITE_USER_JOIN:
123 
124             if (wsm->getInt("sound-fuser-join-use"))
125                 playSound(wsm->getString("sound-fuser-join"));
126         break;
127 
128         case FAVORITE_USER_QUIT:
129 
130             if (wsm->getInt("sound-fuser-quit-use"))
131                 playSound(wsm->getString("sound-fuser-quit"));
132         break;
133 
134         default: break;
135     }
136 }
137 
playSound(const string & target)138 void Sound::playSound(const string &target)
139 {
140 #ifdef USE_LIBGNOME2
141     gnome_sound_play(Text::fromUtf8(target).c_str());
142 #elif USE_LIBCANBERRA
143     ca_context_play(context, 1,CA_PROP_MEDIA_FILENAME, target.c_str(), NULL);
144 #else
145     WulforUtil::openURItoApp(WulforSettingsManager::getInstance()->getString("sound-command") + " \"" +target+"\"");
146 #endif
147 }
148 
sound_finalize()149 void Sound::sound_finalize()
150 {
151 #ifdef USE_LIBGNOME2
152     gnome_sound_shutdown();
153 #elif USE_LIBCANBERRA
154     ca_context_destroy(context);
155 #endif
156 }
157