PDA

View Full Version : Storing the program data in a "safe place"



kremuwa
3rd August 2010, 19:30
Hello.

In my peer2mail client, I've got a "file" class and the objects of this class, appended to QList named fileList represents files that are currently downloaded. Everything's nice, but on the program startup I would like to load these files' data which were in the program when I closed it lately. So this data should be stored somewhere. It would be nice if the user couldn't modify it (I'm not talking about deletion, because I think it's impossible to prevent them from being removed, hm?). So these are my two questions:
1) Where should I store these data and what should I use to store them (which Qt classes for example)?
2) When should I save the data (it includes for example currently downloaded kilobytes of files so it's changing all the time) - create a QTimer and save it every 100 miliseconds? That's also not to accurate...

Thanks in advance :).

squidge
3rd August 2010, 21:42
Keep an in-memory database and flush it to disk once a minute or on program quite.

If you notice an abnormal shutdown, you can take the file, read the downloaded files, compare, and repair the database.

kremuwa
3rd August 2010, 22:23
I'm not sure if I get it. By in-memory database you mean a set of lines of text, each line containing info about one file? Or something else? When I "flush it in to disk" do I have to do something special, to save it as *.dat file? Sorry for my newbism (oh, nice word), it's the first time I'm storing data like that.

sirius
3rd August 2010, 23:53
Take a look at QDataStream (http://doc.qt.nokia.com/latest/qdatastream.html#details) - that's one way of storing your data.

borisbn
4th August 2010, 08:57
Use QSettings and call it's sync() function to actually save data any time you want ( after each write, ones in a minute, etc. )

kremuwa
4th August 2010, 21:27
But should I use QSettings to save such data? AFAIK, QSettings stores the data in the Windows registry (on Windows) - I'm not sure if I should save logins and passwords to mailboxes, or currently downloaded file size there, as plain text. I'd just like to know, how do normal apps store such a data - it can be any download client.

Sirius - that seems reasonable, I will take a look.

ChrisW67
4th August 2010, 23:06
You don't exactly tell us what "such data" contains. Small configuration items, like password (encrypted), user name, server names, etc. are exactly what QSettings is designed for. For a longer list of files then QSettings is not really suitable (although it could be used). The periodic writing of an in-memory list (QList or Sqlite database) to a physical file is a good option. I would store this sort of information in the locations identified by QDesktopServices::DataLocation or QDesktopServices::CacheLocation (see QDesktopServices::storageLocation()) depending on whether I can easily rebuild the file if it were lost. On Windows these are typically in places not seen by the average user.

SixDegrees
5th August 2010, 00:02
Just to expand a bit: if you're going to be storing sensitive information like passwords, it needs to be stored in an encrypted format of some sort. There's no reason you can't use QSettings to do this, but I don't believe QSettings performs any encryption, so that would require a separate step.

kremuwa
5th August 2010, 08:25
The periodic writing of an in-memory list (QList or Sqlite database) to a physical file is a good option.
Do you mean writing using QDataStream? If I'll use QDataStream, do I still have to care about any encryption of the data (since it's binary data after "serialization")?

Thanks a lot for what you've told me already.

sirius
5th August 2010, 10:23
Some ideas:

Have a class managing username+passwords (with a helper function to encrypt/decrypt the passwords) using QSettings. (This is what QSettings is suited for.)

Then have another class managing the files being downloaded; provide a nice interface for your app. Internally it may use a QList+QDataStream or QSqlite (http://doc.qt.nokia.com/4.7-snapshot/sql-driver.html#qsqlite) - whatever suits you best. In this class QSettings is not the best solution IMHO.

BTW - unencrypted data is unencrypted whether it is stored as binary data or as plain text.