Merge pull request #2033 from PhenX/master
[vuplus_xbmc] / xbmc / DbUrl.cpp
1 /*
2  *      Copyright (C) 2012-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program 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, or (at your option)
8  *  any later version.
9  *
10  *  This Program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <sstream>
21
22 #include "DbUrl.h"
23 #include "utils/StringUtils.h"
24 #include "utils/URIUtils.h"
25
26 using namespace std;
27
28 CDbUrl::CDbUrl()
29 {
30   Reset();
31 }
32
33 CDbUrl::~CDbUrl()
34 { }
35
36 void CDbUrl::Reset()
37 {
38   m_valid = false;
39   m_type.clear();
40   m_url.Reset();
41   m_options.clear();
42 }
43
44 std::string CDbUrl::ToString() const
45 {
46   if (!m_valid)
47     return "";
48
49   return m_url.Get();
50 }
51
52 bool CDbUrl::FromString(const std::string &dbUrl)
53 {
54   Reset();
55
56   m_url.Parse(dbUrl);
57   m_valid = parse();
58
59   if (!m_valid)
60     Reset();
61
62   return m_valid;
63 }
64
65 void CDbUrl::AppendPath(const std::string &subPath)
66 {
67   if (!m_valid || subPath.empty())
68     return;
69
70   m_url.SetFileName(URIUtils::AddFileToFolder(m_url.GetFileName(), subPath));
71 }
72
73 void CDbUrl::AddOption(const std::string &key, const char *value)
74 {
75   if (!validateOption(key, value))
76     return;
77   
78   CUrlOptions::AddOption(key, value);
79   updateOptions();
80 }
81
82 void CDbUrl::AddOption(const std::string &key, const std::string &value)
83 {
84   if (!validateOption(key, value))
85     return;
86   
87   CUrlOptions::AddOption(key, value);
88   updateOptions();
89 }
90
91 void CDbUrl::AddOption(const std::string &key, int value)
92 {
93   if (!validateOption(key, value))
94     return;
95   
96   CUrlOptions::AddOption(key, value);
97   updateOptions();
98 }
99
100 void CDbUrl::AddOption(const std::string &key, float value)
101 {
102   if (!validateOption(key, value))
103     return;
104   
105   CUrlOptions::AddOption(key, value);
106   updateOptions();
107 }
108
109 void CDbUrl::AddOption(const std::string &key, double value)
110 {
111   if (!validateOption(key, value))
112     return;
113   
114   CUrlOptions::AddOption(key, value);
115   updateOptions();
116 }
117
118 void CDbUrl::AddOption(const std::string &key, bool value)
119 {
120   if (!validateOption(key, value))
121     return;
122   
123   CUrlOptions::AddOption(key, value);
124   updateOptions();
125 }
126
127 void CDbUrl::AddOptions(const std::string &options)
128 {
129   CUrlOptions::AddOptions(options);
130   updateOptions();  
131 }
132
133 void CDbUrl::RemoveOption(const std::string &key)
134 {
135   CUrlOptions::RemoveOption(key);
136   updateOptions(); 
137 }
138
139 bool CDbUrl::validateOption(const std::string &key, const CVariant &value)
140 {
141   if (key.empty())
142     return false;
143
144   return true;
145 }
146
147 void CDbUrl::updateOptions()
148 {
149   // Update the options string in the CURL object
150   string options = GetOptionsString();
151   if (!options.empty())
152     options = "?" + options;
153
154   m_url.SetOptions(options);
155 }