162 lines
3.2 KiB
C++
162 lines
3.2 KiB
C++
#include "ContactRepository.h"
|
|
#include <QFile>
|
|
|
|
CContactRepository::CContactRepository()
|
|
{
|
|
|
|
}
|
|
|
|
int CContactRepository::LoadContacts()
|
|
{
|
|
QFile* ContactsFile = new QFile("./Configuration/Contacts.mcc");
|
|
if(ContactsFile)
|
|
{
|
|
if(ContactsFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered) == false)
|
|
{
|
|
if(ContactsFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == true)
|
|
{
|
|
mContactsList.clear();
|
|
SaveContacts(); //create an empty file...
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
mContactsList.clear();
|
|
|
|
QDataStream * InputStream = new QDataStream(ContactsFile);
|
|
|
|
quint32 MagicNbr;// = 0xBAADCAFE;
|
|
quint32 NbEntries;
|
|
|
|
*InputStream >> MagicNbr;
|
|
|
|
if(MagicNbr != 0xDEADBEEF)
|
|
{
|
|
return RET_ERROR;
|
|
}
|
|
|
|
*InputStream >> NbEntries;
|
|
|
|
for(unsigned int i = 0; i < NbEntries; i++)
|
|
{
|
|
CContact NewContact;
|
|
*InputStream >> NewContact;
|
|
mContactsList.append(NewContact);
|
|
}
|
|
|
|
qDebug("Loaded %d contacts",mContactsList.size());
|
|
ContactsFile->close();
|
|
delete ContactsFile;
|
|
delete InputStream;
|
|
|
|
return mContactsList.size();
|
|
}
|
|
|
|
int CContactRepository::ChangeContact(int index, CContact Contact)
|
|
{
|
|
if(index < 0 || index >= mContactsList.size())
|
|
{
|
|
return RET_ERROR;
|
|
}
|
|
|
|
mContactsList[index] = Contact;
|
|
SaveContacts();
|
|
return RET_OK;
|
|
}
|
|
|
|
|
|
int CContactRepository::AddContact(CContact Contact)
|
|
{
|
|
|
|
for(int i = 0; i < mContactsList.size(); i++)
|
|
{
|
|
if(mContactsList.at(i) == Contact)
|
|
{
|
|
mContactsList[i] = Contact;
|
|
SaveContacts();
|
|
return RET_OK;
|
|
}
|
|
}
|
|
|
|
mContactsList.append(Contact);
|
|
SaveContacts();
|
|
return RET_OK;
|
|
}
|
|
|
|
int CContactRepository::DeleteContact(int index)
|
|
{
|
|
if(index < 0 || index >= mContactsList.size())
|
|
{
|
|
return RET_ERROR;
|
|
}
|
|
|
|
mContactsList.takeAt(index);
|
|
SaveContacts();
|
|
return RET_OK;
|
|
}
|
|
|
|
int CContactRepository::GetContact(int index,CContact& Contact)
|
|
{
|
|
if(index < 0 || index >= mContactsList.size())
|
|
{
|
|
return RET_ERROR;
|
|
}
|
|
|
|
Contact = mContactsList.at(index);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
CContact *CContactRepository::GetContactPtr(int index)
|
|
{
|
|
if(index < 0 || index >= mContactsList.size())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return &mContactsList[index];
|
|
}
|
|
|
|
int CContactRepository::SaveContacts()
|
|
{
|
|
QFile* ContactsFile = new QFile("./Configuration/Contacts.mcc");
|
|
if(ContactsFile)
|
|
{
|
|
if(ContactsFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Truncate) == false)
|
|
{
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
|
|
QDataStream * InputStream = new QDataStream(ContactsFile);
|
|
|
|
quint32 MagicNbr = 0xDEADBEEF;
|
|
|
|
*InputStream << MagicNbr;
|
|
*InputStream << mContactsList.size();
|
|
|
|
for(int i = 0; i < mContactsList.size(); i++)
|
|
{
|
|
*InputStream << mContactsList.at(i);
|
|
}
|
|
|
|
ContactsFile->close();
|
|
delete ContactsFile;
|
|
delete InputStream;
|
|
|
|
qDebug("SAved %d contacts",mContactsList.size());
|
|
|
|
return mContactsList.size();
|
|
}
|