summaryrefslogtreecommitdiff
path: root/src/DeviceSettings.cpp
blob: dbd3f702cefce55d0887a4e3b42fbc80e8ed9b0b (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
// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
// ***** BEGIN LICENSE BLOCK *****
////////////////////////////////////////////////////////////////////
//                                                                //
// Copyright (c) 2011-2013 RALOVICH, Kristóf                      //
//                                                                //
// This program is free software; you can redistribute it and/or  //
// modify it under the terms of the GNU General Public License    //
// version 2 as published by the Free Software Foundation.        //
//                                                                //
////////////////////////////////////////////////////////////////////
// ***** END LICENSE BLOCK *****


#include "DeviceSettings.hpp"
#include <ctime>
#include <cstring> // memset
#include "common.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "Log.hpp"


namespace antpm {


DeviceSettings::DeviceSettings(const char *devId)
  : mDevId(devId)
{
  loadDefaultValues();
}

void
DeviceSettings::loadDefaultValues()
{
  MaxFileDownloads = 1000;
  struct tm y2k; // 2000-01-01T00:00:00Z 946684800
  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
  y2k.tm_isdst = -1;
  LastUserProfileTime = ::mktime(&y2k) - timezone;
  LastTransferredTime = ::mktime(&y2k) - timezone;
}

const std::string
DeviceSettings::getConfigFileName() const
{
  return getFolder()  + "/config.ini";
}


const std::string
DeviceSettings::getFolder() const
{
  return getConfigFolder() + "/" + mDevId + "/";
}

/// Both inpout and output are represented in GMT/UTC.
std::time_t
DeviceSettings::str2time(const char* from)
{
  if(!from)
    return 0;
  struct tm tm;
  memset(&tm, 0, sizeof(struct tm));
  //strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
  char* rv = ::strptime(from, "%Y-%m-%dT%H:%M:%SZ", &tm);
  bool ok = rv==from+::strlen(from);
  if(!ok)
    return 0;
  return ::mktime(&tm) - timezone;
}

/// Both input and output are represented in GMT/UTC.
const
std::string
DeviceSettings::time2str(const std::time_t t)
{
  char outstr[256];
  memset(outstr, 0, sizeof(outstr));
  struct tm tm;
  memset(&tm, 0, sizeof(struct tm));
  ::gmtime_r(&t, &tm);

  if(::strftime(outstr, sizeof(outstr), "%Y-%m-%dT%TZ", &tm) == 0)
    return "";

  return outstr;
}

bool
DeviceSettings::saveToFile(const char *fname)
{
  boost::property_tree::ptree pt;
  pt.put("antpm.MaxFileDownloads", MaxFileDownloads);
  pt.put("antpm.LastUserProfileTime", time2str(LastUserProfileTime));
  pt.put("antpm.LastTransferredTime", time2str(LastTransferredTime));
  try
  {
    boost::property_tree::ini_parser::write_ini(fname, pt);
  }
  catch(boost::property_tree::ini_parser_error& ipe)
  {
    LOG(LOG_WARN) << ipe.message() << std::endl;
    return false;
  }
  return true;
}

bool DeviceSettings::loadFromFile(const char *fname)
{
  boost::property_tree::ptree pt;
  try
  {
    boost::property_tree::ini_parser::read_ini(fname, pt);
  }
  catch(boost::property_tree::ini_parser_error& ipe)
  {
    LOG(LOG_WARN) << ipe.message() << std::endl;
    return false;
  }

  LOG_VAR(pt.get<unsigned int>("antpm.MaxFileDownloads"));
  LOG_VAR(pt.get<std::string>("antpm.LastUserProfileTime"));
  LOG_VAR(pt.get<std::string>("antpm.LastTransferredTime"));
  MaxFileDownloads    = pt.get<unsigned int>("antpm.MaxFileDownloads");
  LastUserProfileTime = str2time(pt.get<std::string>("antpm.LastUserProfileTime").c_str());
  LastTransferredTime = str2time(pt.get<std::string>("antpm.LastTransferredTime").c_str());
  return true;
}

///
/// \param t expected to be represented as local time
void DeviceSettings::mergeLastUserProfileTime(const std::time_t gmt)
{
  //std::time_t gmt = t + timezone;
  LOG(LOG_DBG) << "LastUserProfileTime: " << time2str(LastUserProfileTime) << " => " << time2str(gmt) << "\n";
  if(gmt > LastUserProfileTime)
  {
    LastUserProfileTime = gmt;
  }
}

///
/// \param t expected to be represented as local time
void DeviceSettings::mergeLastTransferredTime(const std::time_t gmt)
{
  //std::time_t gmt = t + timezone;
  LastTransferredTime = std::max(LastTransferredTime, gmt);
}

}