2012-05-01 16:09:56 -05:00
|
|
|
/*
|
2011-12-03 16:38:03 -06:00
|
|
|
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Copyright 2003-2009, IM Kit Team. All rights reserved.
|
2021-06-17 16:51:37 -05:00
|
|
|
* Copyright 2021, Jaidyn Levesque. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Michael Davidson, slaad@bong.com.au
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
2021-06-17 16:51:37 -05:00
|
|
|
* Jaidyn Levesque, jadedctrl@teknik.io
|
2010-05-07 04:47:10 -05:00
|
|
|
*/
|
|
|
|
|
2021-06-17 16:51:37 -05:00
|
|
|
#include "ProtocolSettings.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
#include <Directory.h>
|
|
|
|
#include <File.h>
|
|
|
|
#include <Message.h>
|
2012-02-25 14:33:28 -06:00
|
|
|
#include <ObjectList.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Path.h>
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "ChatProtocolAddOn.h"
|
|
|
|
#include "Utils.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
ProtocolSettings::ProtocolSettings(ChatProtocolAddOn* addOn)
|
2010-05-19 17:28:26 -05:00
|
|
|
:
|
2021-06-18 01:13:02 -05:00
|
|
|
fTemplate(addOn->Protocol(), "account"),
|
|
|
|
fAddOn(addOn)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
ProtocolSettings::InitCheck() const
|
|
|
|
{
|
|
|
|
return fStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
ChatProtocolAddOn*
|
2010-05-16 16:02:50 -05:00
|
|
|
ProtocolSettings::AddOn() const
|
2010-05-08 14:35:28 -05:00
|
|
|
{
|
2021-06-18 01:13:02 -05:00
|
|
|
return fAddOn;
|
2010-05-08 14:35:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-25 14:33:28 -06:00
|
|
|
BObjectList<BString>
|
2010-05-07 04:47:10 -05:00
|
|
|
ProtocolSettings::Accounts() const
|
|
|
|
{
|
2012-02-25 14:33:28 -06:00
|
|
|
BObjectList<BString> list(true);
|
2010-05-08 14:35:28 -05:00
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature()));
|
2021-06-17 16:51:37 -05:00
|
|
|
|
2010-05-09 04:38:30 -05:00
|
|
|
if (path.InitCheck() != B_OK)
|
2010-05-08 14:35:28 -05:00
|
|
|
return list;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
BDirectory dir(path.Path());
|
|
|
|
BEntry entry;
|
|
|
|
while (dir.GetNextEntry(&entry) == B_OK) {
|
|
|
|
BFile file(&entry, B_READ_ONLY);
|
|
|
|
BMessage msg;
|
|
|
|
|
|
|
|
if (msg.Unflatten(&file) == B_OK) {
|
|
|
|
char buffer[B_PATH_NAME_LENGTH];
|
2010-05-08 14:35:28 -05:00
|
|
|
if (entry.GetName(buffer) == B_OK)
|
2012-02-25 14:33:28 -06:00
|
|
|
list.AddItem(new BString(buffer));
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2010-05-08 14:35:28 -05:00
|
|
|
ProtocolSettings::Load(const char* account, BView* parent)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-08 14:35:28 -05:00
|
|
|
BMessage* settings = NULL;
|
|
|
|
|
|
|
|
if (account) {
|
|
|
|
status_t ret = _Load(account, &settings);
|
|
|
|
if (ret != B_OK)
|
|
|
|
return ret;
|
|
|
|
}
|
2021-06-17 16:51:37 -05:00
|
|
|
return fTemplate.Load(parent, settings);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2021-06-18 12:49:30 -05:00
|
|
|
ProtocolSettings::Save(const char* account, BView* parent, BString* errorText)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
if (!parent)
|
|
|
|
debugger("Couldn't save protocol's settings GUI on a NULL parent!");
|
|
|
|
|
2021-06-18 01:13:02 -05:00
|
|
|
BMessage settings;
|
2021-06-18 12:49:30 -05:00
|
|
|
status_t status = fTemplate.Save(parent, &settings, errorText);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-06-18 12:49:30 -05:00
|
|
|
if (status != B_OK)
|
|
|
|
return status;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-06-17 16:51:37 -05:00
|
|
|
status_t ret = B_ERROR;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-06-17 16:51:37 -05:00
|
|
|
// Find user's settings path
|
2021-06-20 12:44:20 -05:00
|
|
|
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature()));
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-06-17 16:51:37 -05:00
|
|
|
if ((ret = path.InitCheck()) != B_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
// Load settings file
|
|
|
|
path.Append(account);
|
|
|
|
BFile file(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
|
2021-06-18 01:13:02 -05:00
|
|
|
return settings.Flatten(&file);
|
2010-05-08 14:35:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
status_t
|
|
|
|
ProtocolSettings::Rename(const char* from, const char* to)
|
|
|
|
{
|
|
|
|
status_t ret = B_ERROR;
|
|
|
|
|
|
|
|
// Find user's settings path
|
2021-06-20 12:44:20 -05:00
|
|
|
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature()));
|
2021-06-17 16:51:37 -05:00
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
if ((ret = path.InitCheck()) != B_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
path.Append(from);
|
|
|
|
|
|
|
|
// Delete settings file
|
|
|
|
BEntry entry(path.Path());
|
|
|
|
if ((ret = entry.Rename(to)) != B_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-08 14:35:28 -05:00
|
|
|
status_t
|
|
|
|
ProtocolSettings::Delete(const char* account)
|
|
|
|
{
|
|
|
|
status_t ret = B_ERROR;
|
|
|
|
|
|
|
|
// Find user's settings path
|
2021-06-20 12:44:20 -05:00
|
|
|
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature()));
|
2021-06-17 16:51:37 -05:00
|
|
|
|
2010-05-09 04:38:30 -05:00
|
|
|
if ((ret = path.InitCheck()) != B_OK)
|
2010-05-08 14:35:28 -05:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
path.Append(account);
|
|
|
|
|
|
|
|
// Delete settings file
|
|
|
|
BEntry entry(path.Path());
|
|
|
|
if ((ret = entry.Remove()) != B_OK)
|
|
|
|
return ret;
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-08 14:35:28 -05:00
|
|
|
status_t
|
|
|
|
ProtocolSettings::_Load(const char* account, BMessage** settings)
|
|
|
|
{
|
|
|
|
*settings = NULL;
|
|
|
|
|
|
|
|
if (!account || !settings)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
status_t ret = B_ERROR;
|
|
|
|
|
|
|
|
// Find user's settings path
|
2021-06-20 12:44:20 -05:00
|
|
|
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature()));
|
2021-06-17 16:51:37 -05:00
|
|
|
|
2010-05-09 04:38:30 -05:00
|
|
|
if ((ret = path.InitCheck()) != B_OK)
|
2010-05-08 14:35:28 -05:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
// Load settings file
|
|
|
|
path.Append(account);
|
|
|
|
BFile file(path.Path(), B_READ_ONLY);
|
|
|
|
BMessage* msg = new BMessage();
|
|
|
|
ret = msg->Unflatten(&file);
|
|
|
|
*settings = msg;
|
|
|
|
return ret;
|
|
|
|
}
|