1 /***************************************************************************
2 * This file is part of libmygpo-qt                                         *
3 * Copyright (c) 2010 - 2013 Stefan Derkits <stefan@derkits.at>             *
4 * Copyright (c) 2010 - 2011 Christian Wagner <christian.wagner86@gmx.at>   *
5 * Copyright (c) 2010 - 2011 Felix Winter <ixos01@gmail.com>                *
6 *                                                                          *
7 * This library is free software; you can redistribute it and/or            *
8 * modify it under the terms of the GNU Lesser General Public               *
9 * License as published by the Free Software Foundation; either             *
10 * version 2.1 of the License, or (at your option) any later version.       *
11 *                                                                          *
12 * This library is distributed in the hope that it will be useful,          *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
15 * Lesser General Public License for more details.                          *
16 *                                                                          *
17 * You should have received a copy of the GNU Lesser General Public         *
18 * License along with this library; if not, write to the Free Software      *
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
20 * USA                                                                      *
21 ***************************************************************************/
22 
23 
24 #include "UrlBuilder.h"
25 #include "Config.h"
26 
27 #include <QString>
28 #include <QStringBuilder>
29 #include <QLatin1String>
30 
31 using namespace mygpo;
32 
33 const QString UrlBuilder::s_server = Config::instance()->mygpoBaseUrl().toString();
34 const QString UrlBuilder::s_api2 = QLatin1String( "/api/2" );
35 const QString UrlBuilder::s_api1 = QLatin1String( "/api/1" );
36 
getFormatExtension(UrlBuilder::Format f)37 static QString getFormatExtension( UrlBuilder::Format f )
38 {
39     QString ret;
40     switch( f )
41     {
42     case UrlBuilder::JSON:
43         ret =  QString( QLatin1String( ".json" ) );
44         break;
45     case UrlBuilder::OPML:
46         ret =  QString( QLatin1String( ".opml" ) );
47         break;
48     case UrlBuilder::TEXT:
49         ret =  QString( QLatin1String( ".txt" ) );
50         break;
51     case UrlBuilder::XML:
52         ret = QString( QLatin1String( ".xml" ) );
53         break;
54     }
55     return ret;
56 }
57 
getToplistUrl(uint i,Format f)58 QString UrlBuilder::getToplistUrl( uint i, Format f )
59 {
60     QString numString = QString::number(( i == 0 ) ? 1 : i );
61     return s_server % QLatin1String( "/toplist/" ) % numString % getFormatExtension( f );
62 }
63 
getSuggestionsUrl(uint i,Format f)64 QString UrlBuilder::getSuggestionsUrl( uint i, Format f )
65 {
66     QString numString = QString::number(( i == 0 ) ? 1 : i );
67     return s_server % QLatin1String( "/suggestions/" ) % numString % getFormatExtension( f );
68 }
69 
getPodcastSearchUrl(const QString & query,Format f)70 QString UrlBuilder::getPodcastSearchUrl( const QString& query, Format f )
71 {
72     return s_server % QLatin1String( "/search" ) % getFormatExtension( f ) % QLatin1String( "?q=" ) % query;
73 }
74 
getSubscriptionsUrl(const QString & username,const QString & device,UrlBuilder::Format f)75 QString UrlBuilder::getSubscriptionsUrl( const QString& username, const QString& device, UrlBuilder::Format f)
76 {
77     QString deviceString = device.isNull() ? QString(QLatin1String("")) : (QLatin1String( "/" ) % device);
78     return s_server % QLatin1String( "/subscriptions/" ) % username % deviceString % getFormatExtension( f );
79 }
80 
getTopTagsUrl(uint i)81 QString UrlBuilder::getTopTagsUrl( uint i )
82 {
83     QString numString = QString::number(( i == 0 ) ? 1 : i );
84     return s_server % s_api2 % QLatin1String( "/tags/" ) % numString % QLatin1String( ".json" );
85 }
86 
getPodcastsOfTagUrl(const QString & tag,uint i)87 QString UrlBuilder::getPodcastsOfTagUrl( const QString& tag, uint i )
88 {
89     QString numString = QString::number(( i == 0 ) ? 1 : i );
90     return s_server % s_api2 % QLatin1String( "/tag/" ) % tag % QLatin1String( "/" ) % numString % QLatin1String( ".json" );
91 }
92 
getPodcastDataUrl(const QString & url)93 QString UrlBuilder::getPodcastDataUrl( const QString& url )
94 {
95     return s_server % s_api2 % QLatin1String( "/data/podcast" ) % QLatin1String( ".json" ) % QLatin1String( "?url=" ) % url;
96 }
97 
getEpisodeDataUrl(const QString & podcastUrl,const QString & episodeUrl)98 QString UrlBuilder::getEpisodeDataUrl( const QString& podcastUrl, const QString& episodeUrl )
99 {
100     return s_server % s_api2 % QLatin1String( "/data/episode" ) % QLatin1String( ".json" ) % QLatin1String( "?podcast=" ) % podcastUrl % QLatin1String( "&url=" ) % episodeUrl;
101 }
102 
getFavEpisodesUrl(const QString & username)103 QString UrlBuilder::getFavEpisodesUrl( const QString& username )
104 {
105     return s_server % s_api2 % QLatin1String( "/favorites/" ) % username % QLatin1String( ".json" );
106 }
107 
108 
getAddRemoveSubUrl(const QString & username,const QString & deviceId)109 QString UrlBuilder::getAddRemoveSubUrl( const QString& username, const QString& deviceId )
110 {
111     return s_server % s_api2 % QLatin1String( "/subscriptions/" ) % username % QLatin1String( "/" ) % deviceId % QLatin1String( ".json" );
112 }
113 
getAccountSettingsUrl(const QString & username)114 QString UrlBuilder::getAccountSettingsUrl( const QString& username )
115 {
116     return s_server % s_api2 % QLatin1String( "/settings/" ) % username % QLatin1String( "/account" ) % QLatin1String( ".json" );
117 }
118 
getDeviceSettingsUrl(const QString & username,const QString & deviceId)119 QString UrlBuilder::getDeviceSettingsUrl( const QString& username, const QString& deviceId )
120 {
121     return s_server % s_api2 % QLatin1String( "/settings/" ) % username % QLatin1String( "/device" ) % QLatin1String( ".json" ) % QLatin1String( "?device=" ) % deviceId;
122 }
123 
getPodcastSettingsUrl(const QString & username,const QString & podcastUrl)124 QString UrlBuilder::getPodcastSettingsUrl( const QString& username, const QString& podcastUrl )
125 {
126     return s_server % s_api2 % QLatin1String( "/settings/" ) % username % QLatin1String( "/podcast" ) % QLatin1String( ".json" ) % QLatin1String( "?podcast=" ) % podcastUrl;
127 }
128 
getEpisodeSettingsUrl(const QString & username,const QString & podcastUrl,const QString & episodeUrl)129 QString UrlBuilder::getEpisodeSettingsUrl( const QString& username, const QString& podcastUrl, const QString& episodeUrl )
130 {
131     return s_server % s_api2 % QLatin1String( "/settings/" ) % username % QLatin1String( "/episode" ) % QLatin1String( ".json" ) % QLatin1String( "?podcast=" ) % podcastUrl % QLatin1String( "&episode=" ) % episodeUrl;
132 }
133 
getDeviceListUrl(const QString & username)134 QString UrlBuilder::getDeviceListUrl( const QString& username )
135 {
136     return s_server % s_api2 % QLatin1String( "/devices/" ) % username % QLatin1String( ".json" ) ;
137 }
138 
getDeviceUpdatesUrl(const QString & username,const QString & deviceId,qulonglong timestamp)139 QString UrlBuilder::getDeviceUpdatesUrl( const QString& username, const QString& deviceId, qulonglong timestamp )
140 {
141     QString numString = QString::number( timestamp );
142     return s_server % s_api2 % QLatin1String( "/updates/" ) % username % QLatin1String( "/" ) % deviceId % QLatin1String( ".json?since=" ) % numString;
143 }
144 
getRenameDeviceUrl(const QString & username,const QString & deviceId)145 QString UrlBuilder::getRenameDeviceUrl( const QString& username, const QString& deviceId )
146 {
147     return s_server % s_api2 % QLatin1String( "/devices/" ) % username % QLatin1String( "/" ) % deviceId % QLatin1String( ".json" );
148 }
149 
getEpisodeActionsUrl(const QString & username,const bool aggregated)150 QString UrlBuilder::getEpisodeActionsUrl( const QString& username, const bool aggregated )
151 {
152     QString agg;
153     if( aggregated )
154         agg = QLatin1String( "?aggregated=true" );
155     else
156         agg = QLatin1String( "" );
157 
158     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json" ) % agg;
159 }
160 
getEpisodeActionsUrlByPodcast(const QString & username,const QString & podcastUrl,const bool aggregated)161 QString UrlBuilder::getEpisodeActionsUrlByPodcast( const QString& username, const QString& podcastUrl, const bool aggregated )
162 {
163     QString agg;
164     if( aggregated )
165         agg = QLatin1String( "&aggregated=true" );
166     else
167         agg = QLatin1String( "" );
168 
169     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json?podcast=" ) % podcastUrl % agg;
170 }
171 
getEpisodeActionsUrlByDevice(const QString & username,const QString & deviceId,bool aggregated)172 QString UrlBuilder::getEpisodeActionsUrlByDevice( const QString& username, const QString& deviceId, bool aggregated )
173 {
174     QString agg;
175     if( aggregated )
176         agg = QLatin1String( "&aggregated=true" );
177     else
178         agg = QLatin1String( "" );
179 
180     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json?device=" ) % deviceId % agg;
181 }
182 
getEpisodeActionsUrlByTimestamp(const QString & username,qulonglong since)183 QString UrlBuilder::getEpisodeActionsUrlByTimestamp( const QString& username, qulonglong since )
184 {
185     QString numString = QString::number( since );
186     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json?since=" ) % numString;
187 }
188 
getEpisodeActionsUrlByPodcastAndTimestamp(const QString & username,const QString & podcastUrl,qulonglong since)189 QString UrlBuilder::getEpisodeActionsUrlByPodcastAndTimestamp( const QString& username, const QString& podcastUrl, qulonglong since )
190 {
191     QString numString = QString::number( since );
192     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json?podcast=" ) % podcastUrl % QLatin1String( "&since=" ) % numString;
193 }
194 
getEpisodeActionsUrlByDeviceAndTimestamp(const QString & username,const QString & deviceId,qulonglong since)195 QString UrlBuilder::getEpisodeActionsUrlByDeviceAndTimestamp( const QString& username, const QString& deviceId, qulonglong since )
196 {
197     QString numString = QString::number( since );
198     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json?device=" ) % deviceId % QLatin1String( "&since=" ) % numString;
199 }
200 
getUploadEpisodeActionsUrl(const QString & username)201 QString UrlBuilder::getUploadEpisodeActionsUrl( const QString& username )
202 {
203     return s_server % s_api2 % QLatin1String( "/episodes/" ) % username % QLatin1String( ".json" );
204 }
205 
getDeviceSynchronizationStatusUrl(const QString & username)206 QString UrlBuilder::getDeviceSynchronizationStatusUrl ( const QString& username )
207 {
208     return s_server % s_api2 % QLatin1String( "/sync-devices/" ) % username % QLatin1String( ".json" );
209 }
210