vdr  2.2.0
entry.c
Go to the documentation of this file.
1 /*
2  * entry.c: Data structure to handle still pictures
3  *
4  * See the README file for copyright information and how to reach the author.
5  *
6  * $Id: entry.c 3.0 2012/02/17 14:00:28 kls Exp $
7  */
8 
9 #include "entry.h"
10 
11 cPictureEntry::cPictureEntry(const char *Name, const cPictureEntry *Parent, bool IsDirectory)
12 {
13  name = strdup(Name);
14  parent = Parent;
16  entries = NULL;
17 }
18 
20 {
21  free(name);
22  delete entries;
23 }
24 
25 int cPictureEntry::Compare(const cListObject &ListObject) const
26 {
27  cPictureEntry *p = (cPictureEntry *)&ListObject;
28  if (IsDirectory() && !p->IsDirectory())
29  return -1;
30  if (!IsDirectory() && p->IsDirectory())
31  return +1;
32  if (IsDirectory())
33  return strcoll(name, p->name);
34  else
35  return strcmp(name, p->name); // correctly sorts dsc01234.jpg and dsc01234a.jpg in case pictures have been "squeezed in"
36 }
37 
39 {
40  return parent ? *AddDirectory(parent->Path(), name) : name;
41 }
42 
43 void cPictureEntry::Load(void) const
44 {
45  if (isDirectory && !entries) {
46  cString Directory = Path();
47  cReadDir d(Directory);
48  if (d.Ok()) {
49  struct dirent *e;
50  while ((e = d.Next()) != NULL) {
51  struct stat ds;
52  if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) {
53  if (!entries)
55  entries->Add(new cPictureEntry(e->d_name, this, S_ISDIR(ds.st_mode)));
56  }
57  }
58  if (entries)
59  entries->Sort();
60  }
61  else
62  LOG_ERROR_STR(*Directory);
63  }
64 }
65 
67 {
68  Load();
69  return entries;
70 }
71 
73 {
74  Load();
75  if (entries) {
76  for (cPictureEntry *pe = entries->First(); pe; pe = entries->Next(pe)) {
77  if (pe->IsDirectory()) {
78  const cPictureEntry *p = pe->FirstPicture();
79  if (p)
80  return p;
81  }
82  else
83  return pe;
84  }
85  }
86  return NULL;
87 }
88 
90 {
91  Load();
92  if (entries) {
93  for (cPictureEntry *pe = entries->Last(); pe; pe = entries->Prev(pe)) {
94  if (pe->IsDirectory()) {
95  const cPictureEntry *p = pe->LastPicture();
96  if (p)
97  return p;
98  }
99  else
100  return pe;
101  }
102  }
103  return NULL;
104 }
105 
107 {
108  if (This) {
109  const cPictureEntry *pe = (cPictureEntry *)entries->Prev(This);
110  if (pe) {
111  if (pe->IsDirectory()) {
112  const cPictureEntry *p = pe->LastPicture();
113  if (p)
114  return p;
115  return PrevPicture(pe);
116  }
117  return pe;
118  }
119  }
120  if (parent)
121  return parent->PrevPicture(this);
122  return NULL;
123 }
124 
126 {
127  if (This) {
128  cPictureEntry *pe = (cPictureEntry *)entries->Next(This);
129  if (pe) {
130  if (pe->IsDirectory()) {
131  const cPictureEntry *p = pe->FirstPicture();
132  if (p)
133  return p;
134  return NextPicture(pe);
135  }
136  return pe;
137  }
138  }
139  else if (IsDirectory()) {
140  const cPictureEntry *p = FirstPicture();
141  if (p)
142  return p;
143  }
144  if (parent)
145  return parent->NextPicture(this);
146  return NULL;
147 }
struct dirent * Next(void)
Definition: tools.c:1466
const cPictureEntry * FirstPicture(void) const
Definition: entry.c:72
cString AddDirectory(const char *DirName, const char *FileName)
Definition: tools.c:350
const cPictureEntry * LastPicture(void) const
Definition: entry.c:89
virtual int Compare(const cListObject &ListObject) const
Must return 0 if this object is equal to ListObject, a positive value if it is "greater", and a negative value if it is "smaller".
Definition: entry.c:25
void Add(cListObject *Object, cListObject *After=NULL)
Definition: tools.c:2014
const cPictureEntry * NextPicture(const cPictureEntry *This=NULL) const
Definition: entry.c:125
cString Path(void) const
Definition: entry.c:38
const cPictureEntry * PrevPicture(const cPictureEntry *This=NULL) const
Definition: entry.c:106
#define LOG_ERROR_STR(s)
Definition: tools.h:39
void Load(void) const
Definition: entry.c:43
cPictureEntry(const char *Name, const cPictureEntry *Parent, bool IsDirectory)
Definition: entry.c:11
bool IsDirectory(void) const
Definition: entry.h:27
T * Last(void) const
Definition: tools.h:493
const cList< cPictureEntry > * Entries(void) const
Definition: entry.c:66
T * Next(const T *object) const
Definition: tools.h:495
char * name
Definition: entry.h:16
void Sort(void)
Definition: tools.c:2115
bool Ok(void)
Definition: tools.h:379
const cPictureEntry * Parent(void) const
Definition: entry.h:26
cList< cPictureEntry > * entries
Definition: entry.h:19
const cPictureEntry * parent
Definition: entry.h:17
T * First(void) const
Definition: tools.h:492
bool isDirectory
Definition: entry.h:18
T * Prev(const T *object) const
Definition: tools.h:494
virtual ~cPictureEntry()
Definition: entry.c:19
Definition: tools.h:168