Use QProcess::errorString instead
[nomnom.git] / src / Media.cpp
blobc8e6d8f5967d92f8029f720a75da2b9a1cff5017
1 /*
2 * Copyright (C) 2010-2011 Toni Gundogdu.
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 3 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <QScriptValueIterator>
19 #include <QScriptEngine>
20 #include <QVariant>
21 #include <QString>
22 #include <QLabel>
24 #ifdef _0
25 #include <QDebug>
26 #endif
28 #include "Media.h"
30 // Ctor.
32 Media::Media ()
33 : QObject(), _length (0)
34 { }
36 Media::Media (const Media& v)
37 : QObject(), _length(0)
39 _link = v._link;
40 _title = v._title;
41 _pageURL = v._pageURL;
42 _id = v._id;
43 _format = v._format;
44 _length = v._length;
45 _suffix = v._suffix;
46 _ctype = v._ctype;
49 Media&
50 Media::operator=(const Media&)
52 return *this;
55 // Parse from JSON.
57 bool
58 Media::fromJSON (const QString& data, QString& error)
60 QScriptEngine e;
62 QScriptValue v = e.evaluate ("(" +data+ ")");
64 if (e.hasUncaughtException ())
66 error = tr ("Uncaught exception at line %1: %2")
67 .arg (e.uncaughtExceptionLineNumber ())
68 .arg (v.toString ());
69 return false;
72 _title = v.property ("page_title").toString ().simplified ();
73 _pageURL = v.property ("page_url").toString ();
74 _id = v.property ("id").toString ();
75 _host = v.property ("host").toString ();
77 QScriptValueIterator i (v.property ("link"));
79 if ( !i.hasNext () )
81 error = tr("Expected at least one media link from quvi(1), got none.");
82 return false;
85 i.next ();
86 v = i.value ();
88 _length = v.property ("length_bytes").toVariant ().toLongLong ();
89 _ctype = v.property ("content_type").toString ();
90 _suffix = v.property ("file_suffix").toString ();
91 _link = v.property ("url").toString ();
93 #ifdef _0
94 qDebug ()
95 << _length
96 << _ctype
97 << _suffix
98 << _link;
99 #endif
101 return true;
104 // Get.
106 QVariant
107 Media::get (Detail d) const
109 switch (d)
111 case Link :
112 return QVariant (_link);
113 case Title :
114 return QVariant (_title);
115 case PageURL :
116 return QVariant (_pageURL);
117 case ID :
118 return QVariant (_id);
119 case Format :
120 return QVariant (_format);
121 case Length :
122 return QVariant (_length);
123 case Suffix :
124 return QVariant (_suffix);
125 case ContentType:
126 return QVariant (_ctype);
127 case Host :
128 return QVariant(_host);
131 return QVariant();
134 // Set.
136 void
137 Media::set (Detail d, const QString& s)
139 bool ignored = false;
140 switch (d)
142 case Link :
143 _link = s;
144 break;
145 case Title :
146 _title = s;
147 break;
148 case PageURL :
149 _pageURL = s;
150 break;
151 case ID :
152 _id = s;
153 break;
154 case Format :
155 _format = s;
156 break;
157 case Length :
158 _length = s.toLongLong(&ignored);
159 break;
160 case Suffix :
161 _suffix = s;
162 break;
163 case ContentType:
164 _ctype = s;
165 break;
166 case Host :
167 _host = s;
168 break;
169 default :
170 break;
174 // MediaException: ctor.
176 MediaException::MediaException (const QString& errmsg)
177 : errmsg(errmsg)
180 // MediaException: what.
182 const QString&
183 MediaException::what() const
185 return errmsg;
188 // vim: set ts=2 sw=2 tw=72 expandtab: