1/*
2 * This file is a part of BeOS USBVision driver project.
3 * Copyright (c) 2003 by Siarzuk Zharski <imker@gmx.li>
4 *
5 * This file may be used under the terms of the BSD License
6 *
7 */
8#include <Path.h>
9#include <Directory.h>
10#include <Entry.h>
11#include <FindDirectory.h>
12#include <fstream.h>
13
14#include "TunerLocale.h"
15
16using namespace Locale;
17
18const char *kLocalesSettings = "media/usb_vision/Locales";
19
20Channel::Channel(string &str, uint32 frequency):
21                 fName(str), fFrequency(frequency)
22{
23}
24
25const string &Channel::Name() const
26{
27  return fName;
28}
29const uint32 Channel::Frequency()
30{
31  return fFrequency;
32}
33
34TunerLocale::TunerLocale(BEntry &entry)
35{
36  fStatus = B_NO_INIT;
37  if(entry.IsFile()){
38    BPath path;
39    if((fStatus = entry.GetPath(&path)) == B_OK){
40      char name[B_FILE_NAME_LENGTH];
41      if((fStatus = entry.GetName(name)) == B_OK){
42        fName = name;
43        fStatus = LoadLocale(path.Path());
44      }
45    }
46  }
47}
48
49TunerLocale::~TunerLocale()
50{
51}
52
53status_t TunerLocale::InitCheck()
54{
55  return fStatus;
56}
57
58status_t TunerLocale::LoadLocale(const char *name)
59{
60  status_t status = B_OK;
61  ifstream ifs(name);
62  while(ifs.good() && !ifs.eof()){
63    string str;
64    getline(ifs, str);
65    //TODO : validity check and parse strings ...
66    str.erase(0, str.find_first_not_of(" \t"));
67    str.erase(str.find_last_not_of(" \t") + 1);
68    switch(str[0]){
69    case ';': continue;
70    case '@':
71      str.erase(0, 1);
72      fName = str;
73      break;
74    default: //TODO parse ...
75      Channel channel(str, 0);
76      fChannels.push_back(channel);
77      break;
78    }
79  }
80  return status;
81}
82
83status_t TunerLocale::LoadLocales(vector<TunerLocale *> &vector)
84{
85  status_t status = B_ERROR;
86  BPath path;
87  if((status = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) != B_OK)
88    return status;
89  if((status = path.Append(kLocalesSettings)) != B_OK)
90    return status;
91  BDirectory directory(path.Path());
92  if((status = directory.InitCheck()) != B_OK)
93    return status;
94  vector.clear();
95  BEntry entry;
96  while((status = directory.GetNextEntry(&entry)) == B_OK){
97    if(entry.IsFile()){
98      TunerLocale *locale = new TunerLocale(entry);
99      if((status = locale->InitCheck()) == B_OK){
100        vector.push_back(locale);
101      }
102      else
103        delete locale;
104    }
105  }
106  if(status == B_ENTRY_NOT_FOUND) //we reached the end of entries list ...
107    status = B_OK;
108  return status;
109}
110
111status_t TunerLocale::ReleaseLocales(vector<TunerLocale *> &vector)
112{
113  for(LocalesIterator locale = vector.begin();
114               locale != vector.end(); locale ++){
115    delete *locale;
116  }
117  return B_OK;
118}
119
120const string &TunerLocale::Name()
121{
122  return fName;
123}
124
125const Channel &TunerLocale::GetChannel(int idx)
126{
127  if(idx < 0) idx = 0;
128  if(idx >= (int)fChannels.size()) idx = (int)(fChannels.size() - 1);
129  return fChannels[idx];
130}
131
132uint32 TunerLocale::ChannelsCount()
133{
134  return fChannels.size();
135}
136