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