summaryrefslogtreecommitdiff
path: root/media/mediaManager.cpp
blob: ed07da73299ab164d4d8151e91815bc7f1c6eaf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * mediaManager.cpp
 *
 *  Created on: 01.09.2012
 *      Author: savop
 */

#include "../include/plugin.h"
#include "../include/media/mediaManager.h"
#include "../include/server.h"
#include <upnp/upnp.h>
#include <sstream>
#include <tntdb/statement.h>
#include <tntdb/result.h>

namespace upnp {

cMediaManager::cMediaManager()
: mSystemUpdateID(0)
, mDatabaseFile("metadata.db")
{
}

cMediaManager::~cMediaManager(){
}

uint32_t cMediaManager::GetSystemUpdateID() const {
  return mSystemUpdateID;
}

IdList cMediaManager::GetContainerUpdateIDs(bool unevented){
  IdList list = mEventedContainerUpdateIDs;

  if(!unevented)
    mEventedContainerUpdateIDs.clear();

  return list;
}

void cMediaManager::OnContainerUpdate(string containerID, long updateID){
  ++mSystemUpdateID;

  mEventedContainerUpdateIDs[containerID] = updateID;
}

StringList cMediaManager::GetSearchCapabilities() const {
  StringList list;

  list.push_back("dc:title");
  list.push_back("dc:creator");
  list.push_back("dc:description");
  list.push_back("upnp:longDescription");
  list.push_back("res@protocolInfo");
  list.push_back("upnp:class");
  list.push_back("dc:date");
  list.push_back("dc:language");

  return list;
}

StringList cMediaManager::GetSortCapabilities() const {
  StringList list;

  list.push_back("dc:title");
  list.push_back("dc:creator");
  list.push_back("dc:description");
  list.push_back("upnp:longDescription");
  list.push_back("res@protocolInfo");
  list.push_back("upnp:class");
  list.push_back("dc:date");
  list.push_back("dc:language");

  return list;
}

StringList cMediaManager::GetSupportedProtocolInfos() const {
  tntdb::Connection conn = mConnection;
  tntdb::Statement stmt = conn.prepare(
      "SELECT DISTINCT protocolInfo FROM resources;"
      );

  StringList list;

  for(tntdb::Statement::const_iterator it = stmt.begin(); it != stmt.end(); ++it){
    tntdb::Row row = (*it);
    list.push_back(row.getString("protocolInfo"));
  }

  return list;
}

int cMediaManager::Browse(BrowseRequest& request){
  request.numberReturned = 0;
  request.totalMatches = 0;
  request.updateID = 0;

  return UPNP_E_SUCCESS;
}

int cMediaManager::Search(SearchRequest& request){
  request.numberReturned = 0;
  request.totalMatches = 0;
  request.updateID = 0;

  return UPNP_E_SUCCESS;
}

cMediaManager::BrowseFlag cMediaManager::ToBrowseFlag(std::string browseFlag) {
  if      (browseFlag.compare("BrowseMetadata") == 0)
    return CD_BROWSE_METADATA;
  else if (browseFlag.compare("BrowseDirectChildren") == 0)
    return CD_BROWSE_DIRECT_CHILDREN;
  else
    return NumBrowseFlags;
}

bool cMediaManager::Initialise(){
  try {
    stringstream ss;
    ss << "sqlite:" << mDatabaseFile;

    mConnection = tntdb::connect(ss.str());

    dsyslog("UPNP\tPreparing database structure...");

    if(CheckIntegrity()) return true;

    mConnection.beginTransaction();

    tntdb::Statement objectTable = mConnection.prepare(
        "CREATE TABLE metadata"
        "("
        "  objectID       TEXT    PRIMARY KEY,"
        "  parentID       TEXT    NOT NULL,"
        "  title          TEXT    NOT NULL,"
        "  class          TEXT    NOT NULL,"
        "  restricted     INTEGER NOT NULL,"
        "  description    TEXT,"
        "  ldescription   TEXT,"
        "  date           TEXT,"
        "  language       TEXT,"
        "  channelNr      INTEGER,"
        "  channelName    TEXT,"
        "  scheduledStart TEXT,"
        "  scheduledEnd   TEXT"
        ")");

    objectTable.execute();

    tntdb::Statement detailsTable = mConnection.prepare(
        "CREATE TABLE details"
        "("
        "  propertyID INTEGER PRIMARY KEY,"
        "  objectID   TEXT    REFERENCES metadata (objectID) ON DELETE CASCADE ON UPDATE CASCADE,"
        "  property   TEXT,"
        "  value      TEXT"
        ")");

    detailsTable.execute();

    tntdb::Statement resourcesTable = mConnection.prepare(
        "CREATE TABLE resources"
        "("
        "  resourceID       INTEGER PRIMARY KEY,"
        "  objectID         TEXT    REFERENCES metadata (objectID) ON DELETE CASCADE ON UPDATE CASCADE,"
        "  resourceUri      TEXT    NOT NULL,"
        "  protocolInfo     TEXT    NOT NULL,"
        "  size             INTEGER,"
        "  duration         TEXT,"
        "  resolution       TEXT,"
        "  bitrate          INTEGER,"
        "  sampleFreq       INTEGER,"
        "  bitsPerSample    INTEGER,"
        "  nrAudioChannels  INTEGER,"
        "  colorDepth       INTEGER"
        ")");

    resourcesTable.execute();

    tntdb::Statement rootContainer = mConnection.prepare(
        "INSERT INTO metadata (objectID,  parentID,  title,  class,  restricted,  description)"
        "              VALUES (:objectID, :parentID, :title, :class, :restricted, :description)"
        );

    rootContainer.setString("objectID", "0")
                 .setString("parentID", "-1")
                 .setString("title", cMediaServer::GetInstance()->GetServerDescription().friendlyName)
                 .setString("class", "object.container")
                 .setBool("restricted", true)
                 .setString("description", cMediaServer::GetInstance()->GetServerDescription().modelDescription)
                 .execute();

    mConnection.commitTransaction();

    return true;

  } catch (const std::exception& e) {
    esyslog("UPnP\tException occurred while initializing database: %s", e.what());

    mConnection.rollbackTransaction();

    return false;
  }

  return false;
}

bool cMediaManager::CheckIntegrity(){

  tntdb::Statement checkTable = mConnection.prepare(
          "SELECT name FROM sqlite_master WHERE type='table' AND name=:table;"
          );

  if( checkTable.setString("table", "metadata").select().empty() ) return false;
  if( checkTable.setString("table", "details").select().empty() ) return false;
  if( checkTable.setString("table", "resources").select().empty() ) return false;

  tntdb::Statement checkObject = mConnection.prepare(
          "SELECT objectID FROM metadata WHERE objectID='0' AND parentID='-1';"
          );

  if( checkObject.select().size() != 1 ) return false;

  return true;
}

void cMediaManager::SetDatabaseFile(string file){
  if(file.empty()) mDatabaseFile = "metadata.db";
  else mDatabaseFile = file;
}

void cMediaManager::Action(){

}

}  // namespace upnp