svvitch
digital signage player
D:/vs_workspace/switch_sf/src/svvitch/MediaItem.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <Poco/Logger.h>
00004 #include <vector>
00005 #include <Poco/File.h>
00006 #include <Poco/format.h>
00007 #include <Poco/NumberParser.h>
00008 #include <Poco/HashMap.h>
00009 #include <Poco/String.h>
00010 #include <Poco/Stringtokenizer.h>
00011 
00012 #include "Common.h"
00013 
00014 using Poco::File;
00015 using std::string;
00016 using std::vector;
00017 
00018 
00022 enum MediaType {
00023     MediaTypeMix,
00024     MediaTypeMovie,
00025     MediaTypeImage,
00026     MediaTypeText,
00027     MediaTypeFlash,
00028     MediaTypeBrowser,
00029     MediaTypeCv,
00030     MediaTypeCvCap,
00031     MediaTypeGame,
00032     MediaTypeGame2,
00033     MediaTypeUnknown,
00034 };
00035 
00036 
00041 class MediaItemFile
00042 {
00043 private:
00044     const string NULL_STRING;
00045     Poco::Logger& _log;
00046     MediaType _type;
00047     string _file;
00048     string _params;
00049     Poco::HashMap<string, string> _properties;
00050 
00051     MediaItemFile& copy(const MediaItemFile& mif) {
00052         if (this == &mif) return *this;
00053         _type = mif._type;
00054         _file = mif._file;
00055         _params = mif._params;
00056         _properties = mif._properties;
00057         return *this;
00058     }
00059 
00060 public:
00061     MediaItemFile(const MediaType type, const string file, const string params): _log(Poco::Logger::get("")),
00062         _type(type), _file(file), _params(params)
00063     {
00064         if (!params.empty()) {
00065 //          _log.information(Poco::format("properties: %s", file));
00066 //          _file = file.substr(0, file.find("?"));
00067 //          string params = file.substr(file.find("?") + 1);
00068             Poco::StringTokenizer props(params, ",");
00069             for (Poco::StringTokenizer::Iterator it = props.begin(); it != props.end(); it++) {
00070                 string pair = *it;
00071                 int i = pair.find("=");
00072                 if (i != string::npos) {
00073                     string key = Poco::trim(pair.substr(0, i));
00074                     string value = Poco::trim(pair.substr(i + 1));
00075                     _properties[key] = value;
00076 //                  _log.information(Poco::format("property[%s]=<%s>", key, value));
00077                 }
00078             }
00079         }
00080     }
00081 
00082     MediaItemFile(const MediaItemFile& mif): _log(Poco::Logger::get("")) {
00083         copy(mif);
00084     }
00085 
00086     MediaItemFile& operator=(const MediaItemFile& mif) {
00087         return copy(mif);
00088     }
00089 
00090     virtual ‾MediaItemFile() {
00091         // _properties.clear();
00092     }
00093 
00094     const MediaType type() const {
00095         return _type;
00096     }
00097 
00098     const string& file() const {
00099         return _file;
00100     }
00101 
00102     const string& params() const {
00103         return _params;
00104     }
00105 
00106     const string& getProperty(const string& key) const {
00107         if (_properties.find(key) != _properties.end()) {
00108             return _properties[key];
00109         }
00110         return NULL_STRING;
00111     }
00112 
00113     const string& getProperty(const string& key, const string& defaultValue) const {
00114         if (_properties.find(key) != _properties.end()) {
00115             return _properties[key];
00116         }
00117         return defaultValue;
00118     }
00119 
00120     const int getNumProperty(const string& key, const int& defaultValue) const {
00121         int num = defaultValue;
00122         Poco::NumberParser::tryParse(getProperty(key), num);
00123         return num;
00124     }
00125 
00126     const DWORD getHexProperty(const string& key, const DWORD& defaultValue) const {
00127         Poco::UInt64 num = defaultValue;
00128         Poco::NumberParser::tryParseHex64(getProperty(key), num);
00129         return (DWORD)num;
00130     }
00131 
00132     const double getFloatProperty(const string& key, const double& defaultValue) const {
00133         double num = defaultValue;
00134         Poco::NumberParser::tryParseFloat(getProperty(key), num);
00135         return num;
00136     }
00137 };
00138 
00139 
00144 class MediaItem
00145 {
00146 private:
00147     const string NULL_STRING;
00148     Poco::Logger& _log;
00149 
00150     MediaType _type;
00151     string _id;
00152     string _name;
00153     int _start;
00154     int _duration;
00155     string _params;
00156     Poco::HashMap<string, string> _properties;
00157     vector<MediaItemFile> _files;
00158 
00159     MediaItem& copy(const MediaItem& item) {
00160         if (this == &item) return *this;
00161         _type = item._type;
00162         _id = item._id;
00163         _name = item._name;
00164         _start = item._start;
00165         _duration = item._duration;
00166         _params = item._params;
00167         _properties = item._properties;
00168         _files = item._files;
00169         return *this;
00170     }
00171 
00172 public:
00173     MediaItem(const MediaType type, const string id, const string name, const int start, const int duration, const string params, const vector<MediaItemFile> files):
00174         _log(Poco::Logger::get("")), _type(type), _id(id), _name(name), _start(start), _duration(duration), _params(params), _files(files)
00175     {
00176         if (!params.empty()) {
00177             Poco::StringTokenizer props(params, ",");
00178             for (Poco::StringTokenizer::Iterator it = props.begin(); it != props.end(); it++) {
00179                 string pair = *it;
00180                 int i = pair.find("=");
00181                 if (i != string::npos) {
00182                     string key = Poco::trim(pair.substr(0, i));
00183                     string value = Poco::trim(pair.substr(i + 1));
00184                     _properties[key] = value;
00185 //                  _log.information(Poco::format("property[%s]=<%s>", key, value));
00186                 }
00187             }
00188         }
00189     }
00190 
00191     MediaItem(const MediaItem& item): _log(Poco::Logger::get("")) {
00192         copy(item);
00193     }
00194 
00195     MediaItem& operator=(const MediaItem& mif) {
00196         return copy(mif);
00197     }
00198 
00199     virtual ‾MediaItem(void) {
00200 //      for (vector<MediaItemFile>::iterator it = _files.begin(); it != _files.end(); it++) SAFE_DELETE(*it);
00201         _files.clear();
00202         _properties.clear();
00203     }
00204 
00205     const MediaType type() const {
00206         return _type;
00207     }
00208 
00209     const bool containsFileType(const MediaType type) {
00210         for (vector<MediaItemFile>::iterator it = _files.begin(); it != _files.end(); it++) {
00211             if ((*it).type() == type) return true;
00212         }
00213         return false;
00214     }
00215 
00216     const string& id() const {
00217         return _id;
00218     }
00219 
00220     const string& name() const {
00221         return _name;
00222     }
00223 
00224     const int start() const {
00225         return _start;
00226     }
00227 
00228     const int duration() const {
00229         return _duration;
00230     }
00231 
00232     const string& params() const {
00233         return _params;
00234     }
00235 
00236     const string& getProperty(const string& key) const {
00237         if (_properties.find(key) != _properties.end()) {
00238             return _properties[key];
00239         }
00240         return NULL_STRING;
00241     }
00242 
00243     const string& getProperty(const string& key, const string& defaultValue) const {
00244         if (_properties.find(key) != _properties.end()) {
00245             return _properties[key];
00246         }
00247         return defaultValue;
00248     }
00249 
00250     const int getNumProperty(const string& key, const int& defaultValue) const {
00251         int num = defaultValue;
00252         Poco::NumberParser::tryParse(getProperty(key), num);
00253         return num;
00254     }
00255 
00256     const DWORD getHexProperty(const string& key, const DWORD& defaultValue) const {
00257         Poco::UInt64 num = defaultValue;
00258         Poco::NumberParser::tryParseHex64(getProperty(key), num);
00259         return (DWORD)num;
00260     }
00261 
00262     const double getFloatProperty(const string& key, const double& defaultValue) const {
00263         double num = defaultValue;
00264         Poco::NumberParser::tryParseFloat(getProperty(key), num);
00265         return num;
00266     }
00267 
00268     const int fileCount() {
00269         return _files.size();
00270     }
00271 
00272     const vector<MediaItemFile>& files() {
00273         return _files;
00274     }
00275 };
00276 
00277 
00278 typedef MediaItem* MediaItemPtr;
00279 typedef MediaItemFile* MediaItemFilePtr;
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines