LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
syncdeltamanager.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "syncdeltamanager.h"
31 #include <stdexcept>
32 #include <algorithm>
33 #include <QFile>
34 #include <QCoreApplication>
35 #include <util/util.h>
36 #include <util/sync/syncops.h>
37 
38 namespace LeechCraft
39 {
40  namespace Util
41  {
42  SyncDeltaManager::SyncDeltaManager (const QString& id, QObject *parent)
43  : QObject (parent)
44  , ID_ (id)
45  , Settings_ (QCoreApplication::organizationName (),
46  QCoreApplication::applicationName ())
47  {
48  Settings_.beginGroup ("SyncDeltaManager");
49  Settings_.beginGroup (id);
50  }
51 
53  {
54  Settings_.endGroup ();
55  Settings_.endGroup ();
56  }
57 
58  void SyncDeltaManager::Store (const Sync::ChainID_t& chainId, const Sync::Payload& payload)
59  {
60  Settings_.beginGroup (chainId);
61  bool shouldStore = Settings_.value ("EverRequested", false).toBool ();
62  Settings_.endGroup ();
63  if (!shouldStore)
64  return;
65 
66  QDir dir = GetDir (chainId);
67  int curNum = GetLastFileNum (chainId) + 1;
68 
69  StoreImpl (dir.absoluteFilePath (QString::number (curNum)), payload);
70 
71  SetLastFileNum (chainId, curNum);
72  }
73 
74  void SyncDeltaManager::Store (const Sync::ChainID_t& chainId, const Sync::Payloads_t& payloads)
75  {
76  Settings_.beginGroup (chainId);
77  bool shouldStore = Settings_.value ("EverRequested", false).toBool ();
78  Settings_.endGroup ();
79  if (!shouldStore)
80  return;
81 
82  QDir dir = GetDir (chainId);
83  int curNum = GetLastFileNum (chainId);
84 
85  Q_FOREACH (const Sync::Payload& payload, payloads)
86  StoreImpl (dir.absoluteFilePath (QString::number (++curNum)), payload);
87 
88  SetLastFileNum (chainId, curNum);
89  }
90 
92  {
93  DeltasRequested (chainId);
94 
95  QMap<int, Sync::Payload> tmpPayloads;
96 
97  QDir dir = GetDir (chainId);
98 
99  Q_FOREACH (const QString& filename, dir.entryList (QDir::Files | QDir::NoDotAndDotDot))
100  {
101  bool ok = true;
102  int num = filename.toInt (&ok);
103  if (!ok)
104  continue;
105 
106  QFile file (dir.absoluteFilePath (filename));
107  if (!file.open (QIODevice::ReadOnly))
108  {
109  qWarning () << Q_FUNC_INFO
110  << "unable to open"
111  << file.fileName ()
112  << "for reading:"
113  << file.errorString ();
114  throw std::runtime_error ("Unable to open file for reading.");
115  }
116 
117  QByteArray data = file.readAll ();
118  Sync::Payload payload = Sync::Deserialize (qUncompress (data));
119  tmpPayloads [num] = payload;
120  }
121 
122  QList<Sync::Payload> result;
123  QList<int> keys = tmpPayloads.keys ();
124  std::sort (keys.begin (), keys.end ());
125  Q_FOREACH (int key, keys)
126  result << tmpPayloads [key];
127 
128  return result;
129  }
130 
131  void SyncDeltaManager::Purge (const Sync::ChainID_t& chainId, quint32 num)
132  {
133  QDir dir = GetDir (chainId);
134  quint32 purged = 0;
135  Q_FOREACH (const QString& filename, dir.entryList (QDir::Files | QDir::NoDotAndDotDot))
136  {
137  if (!dir.remove (filename))
138  qWarning () << Q_FUNC_INFO
139  << "could not remove"
140  << filename;
141  if (++purged == num)
142  break;
143  }
144  }
145 
147  {
148  Settings_.beginGroup (chainId);
149  Settings_.setValue ("EverRequested", true);
150  Settings_.endGroup ();
151  }
152 
153  QDir SyncDeltaManager::GetDir (const Sync::ChainID_t& chainId) const
154  {
155  return Util::CreateIfNotExists ("deltastorage/" + ID_ + "/" + chainId);
156  }
157 
158  int SyncDeltaManager::GetLastFileNum (const Sync::ChainID_t& chainId)
159  {
160  Settings_.beginGroup (chainId);
161  int num = Settings_.value ("LastFileNum", 0).toInt ();
162  Settings_.endGroup ();
163  return num;
164  }
165 
166  void SyncDeltaManager::SetLastFileNum (const Sync::ChainID_t& chainId, int num)
167  {
168  Settings_.beginGroup (chainId);
169  Settings_.setValue ("LastFileNum", num);
170  Settings_.endGroup ();
171  }
172 
173  void SyncDeltaManager::StoreImpl (const QString& path, const Sync::Payload& payload)
174  {
175  QFile file (path);
176  if (!file.open (QIODevice::WriteOnly))
177  {
178  qWarning () << Q_FUNC_INFO
179  << "unable to open file"
180  << file.fileName ()
181  << "for writing:"
182  << file.errorString ();
183  throw std::runtime_error ("Unable to open file for writing.");
184  }
185 
186  file.write (qCompress (Sync::Serialize (payload), 5));
187  }
188  }
189 }
190 
Sync::Payloads_t Get(const Sync::ChainID_t &)
Payload Deserialize(const QByteArray &data)
Definition: syncops.cpp:80
void Purge(const Sync::ChainID_t &, quint32 num)
QByteArray Serialize(const Payload &payload)
Definition: syncops.cpp:69
QByteArray ChainID_t
Definition: isyncable.h:56
void DeltasRequested(const Sync::ChainID_t &)
UTIL_API QDir CreateIfNotExists(const QString &path)
Creates a path if it doesn&#39;t exist.
Definition: util.cpp:233
QList< Payload > Payloads_t
Definition: isyncable.h:46
SyncDeltaManager(const QString &, QObject *=0)
void Store(const Sync::ChainID_t &, const Sync::Payload &)