1 |
// Media Folder |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <dbi/resultset.hpp> |
10 |
|
11 |
#include "MediaFolder.hpp" |
12 |
#include "Share.hpp" |
13 |
|
14 |
MediaFolder::MediaFolder(const _R<Connector>& connector, const api::Path& path) : connector(connector), path(path) |
15 |
{ |
16 |
_R<dbi::Connection> connection(connector->Connect()); |
17 |
_R<dbi::ResultSet> root_(connection->Parse(_B("SELECT root FROM folders WHERE path = ?"))->Execute(path.GetPath())); |
18 |
|
19 |
if (root_->MoveNext()) |
20 |
root = root_->GetString(_B("root")); |
21 |
|
22 |
connector->Release(connection); |
23 |
} |
24 |
|
25 |
MediaFolder::MediaFolder(const _R<Connector>& connector, const api::Path& path, const api::Path& root) : connector(connector), path(path), root(root) |
26 |
{ |
27 |
_R<dbi::Connection> connection(connector->Connect()); |
28 |
_R<dbi::ResultSet> path_(connection->Parse(_B("SELECT path FROM folders WHERE path = ?"))->Execute(path.GetPath())); |
29 |
|
30 |
if (path_->MoveNext()) |
31 |
if (path.GetPath() != root.GetPath()) |
32 |
connection->Parse(_B("UPDATE folders SET folder = ?, root = ? WHERE path = ?"))->Execute(path.GetParent().GetPath(), root.GetPath(), path.GetPath()); |
33 |
else |
34 |
connection->Parse(_B("UPDATE folders SET folder = NULL, root = ? WHERE path = ?"))->Execute(root.GetPath(), path.GetPath()); |
35 |
else |
36 |
if (path.GetPath() != root.GetPath()) |
37 |
connection->Parse(_B("INSERT INTO folders (path, folder, root) VALUES (?, ?, ?)"))->Execute(path.GetPath(), path.GetParent().GetPath(), root.GetPath()); |
38 |
else |
39 |
connection->Parse(_B("INSERT INTO folders (path, folder, root) VALUES (?, NULL, ?)"))->Execute(path.GetPath(), root.GetPath()); |
40 |
|
41 |
connector->Release(connection); |
42 |
} |
43 |
|
44 |
cse::String MediaFolder::GetName() const |
45 |
{ |
46 |
if (path == root && path.GetPath().StartsWithAll(Share::shares.GetPath())) |
47 |
{ |
48 |
api::Address address(api::Address::Resolve(path.GetParent().GetName(), _B("6996")).First()); |
49 |
cse::String host, port; |
50 |
|
51 |
address.ToString(host, port, true); |
52 |
|
53 |
return _S<ios::String>() << _B("Share from ") << host; |
54 |
} |
55 |
else |
56 |
return path.GetName(); |
57 |
} |
58 |
|
59 |
_L<MediaFile> MediaFolder::GetFiles(unsigned page) const |
60 |
{ |
61 |
_R<dbi::Connection> connection(connector->Connect()); |
62 |
_R<dbi::ResultSet> paths(connection->Parse(_B("SELECT path FROM files WHERE live = TRUE AND folder = ? ORDER BY path LIMIT 50 OFFSET ?"))->Execute(path.GetPath(), page * 50)); |
63 |
_L<MediaFile> files; |
64 |
|
65 |
while (paths->MoveNext()) |
66 |
files.InsertLast(MediaFile(connector, paths->GetString(_B("path")))); |
67 |
|
68 |
connector->Release(connection); |
69 |
|
70 |
return files; |
71 |
} |
72 |
|
73 |
unsigned MediaFolder::GetFilesPages() const |
74 |
{ |
75 |
_R<dbi::Connection> connection(connector->Connect()); |
76 |
_R<dbi::ResultSet> count(connection->Parse(_B("SELECT count(path) FROM files WHERE live = TRUE AND folder = ?"))->Execute(path.GetPath())); |
77 |
unsigned pages(0); |
78 |
|
79 |
if (count->MoveNext()) |
80 |
pages = (count->Get<unsigned>(_B("count")) + 49) / 50; |
81 |
|
82 |
connector->Release(connection); |
83 |
|
84 |
return pages; |
85 |
} |
86 |
|
87 |
_L<MediaFolder> MediaFolder::GetFolders(unsigned page) const |
88 |
{ |
89 |
_R<dbi::Connection> connection(connector->Connect()); |
90 |
_R<dbi::ResultSet> paths(connection->Parse(_B("SELECT path FROM folders WHERE folder = ? ORDER BY path LIMIT 50 OFFSET ?"))->Execute(path.GetPath(), page * 50)); |
91 |
_L<MediaFolder> folders; |
92 |
|
93 |
while (paths->MoveNext()) |
94 |
folders.InsertLast(MediaFolder(connector, paths->GetString(_B("path")))); |
95 |
|
96 |
connector->Release(connection); |
97 |
|
98 |
return folders; |
99 |
} |
100 |
|
101 |
unsigned MediaFolder::GetFoldersPages() const |
102 |
{ |
103 |
_R<dbi::Connection> connection(connector->Connect()); |
104 |
_R<dbi::ResultSet> count(connection->Parse(_B("SELECT count(path) FROM folders WHERE folder = ?"))->Execute(path.GetPath())); |
105 |
unsigned pages(0); |
106 |
|
107 |
if (count->MoveNext()) |
108 |
pages = (count->Get<unsigned>(_B("count")) + 49) / 50; |
109 |
|
110 |
connector->Release(connection); |
111 |
|
112 |
return pages; |
113 |
} |