Fixed at least the most obvious violations of our style guide...

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12324 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-04-12 04:15:15 +00:00
parent 79de26ad08
commit c7def18e6d

View File

@ -20,300 +20,337 @@ void GetSubFolders(BDirectory *of, BStringList *folders, const char *prepend = "
class UpdateHandler : public BHandler { class UpdateHandler : public BHandler {
public: public:
UpdateHandler(BRemoteMailStorageProtocol *prot, const char *dest) : _prot(prot), _dest(dest) { UpdateHandler(BRemoteMailStorageProtocol *prot, const char *dest)
: fProtocol(prot), fDestination(dest)
{
node_ref ref; node_ref ref;
_dest.GetNodeRef(&ref); fDestination.GetNodeRef(&ref);
dest_node = ref.node; fDestinationNode = ref.node;
} }
virtual ~UpdateHandler() {
virtual ~UpdateHandler()
{
stop_watching(this); stop_watching(this);
} }
void MessageReceived(BMessage *msg) {
void MessageReceived(BMessage *msg)
{
switch (msg->what) { switch (msg->what) {
case 'INIT': { case 'INIT': {
if (_prot->InitCheck() < B_OK) if (fProtocol->InitCheck() < B_OK)
return; return;
((BMailChainRunner *)(Looper()))->ReportProgress(0,0,"Synchronizing Mailboxes"); ((BMailChainRunner *)(Looper()))->ReportProgress(0, 0,
"Synchronizing Mailboxes");
BStringList subdirs; BStringList subdirs;
GetSubFolders(&_dest,&subdirs); GetSubFolders(&fDestination, &subdirs);
BStringList to_delete; BStringList to_delete;
BStringList to_add; BStringList to_add;
subdirs.NotThere(_prot->mailboxes,&to_add); subdirs.NotThere(fProtocol->mailboxes, &to_add);
if (subdirs.CountItems() != 0) // --- If it's a virgin mailfolder, the user probably just configured his machineand probably *doesn't* want all his mail folders deleted :) if (subdirs.CountItems() != 0) {
subdirs.NotHere(_prot->mailboxes,&to_delete); // If it's a virgin mailfolder, the user probably just configured
// his machine and probably *doesn't* want all his mail folders deleted :)
subdirs.NotHere(fProtocol->mailboxes, &to_delete);
}
for (int32 i = 0; i < to_add.CountItems(); i++) { for (int32 i = 0; i < to_add.CountItems(); i++) {
if (_prot->CreateMailbox(to_add[i]) != B_OK) if (fProtocol->CreateMailbox(to_add[i]) != B_OK)
continue; continue;
_prot->mailboxes += to_add[i];
_prot->SyncMailbox(to_add[i]); fProtocol->mailboxes += to_add[i];
fProtocol->SyncMailbox(to_add[i]);
} }
_prot->CheckForDeletedMessages(); //--- Refresh the manifest list, delete messages in locally deleted folders fProtocol->CheckForDeletedMessages();
// Refresh the manifest list, delete messages in locally deleted folders
for (int32 i = 0; i < to_delete.CountItems(); i++) { for (int32 i = 0; i < to_delete.CountItems(); i++) {
if (to_delete[i][0] == 0) if (to_delete[i][0] == 0)
continue; continue;
if (_prot->DeleteMailbox(to_delete[i]) == B_OK) if (fProtocol->DeleteMailbox(to_delete[i]) == B_OK)
_prot->mailboxes -= to_delete[i]; fProtocol->mailboxes -= to_delete[i];
} }
entry_ref ref; entry_ref ref;
BEntry entry; BEntry entry;
_dest.GetEntry(&entry); fDestination.GetEntry(&entry);
entry.GetRef(&ref); entry.GetRef(&ref);
BPath path(&ref), work_path(path); BPath path(&ref), work_path(path);
BNode node(&ref); BNode node(&ref);
node_ref watcher; node_ref watcher;
node.GetNodeRef(&watcher); node.GetNodeRef(&watcher);
watch_node(&watcher,B_WATCH_DIRECTORY,this); watch_node(&watcher,B_WATCH_DIRECTORY,this);
for (int32 i = 0; i < _prot->mailboxes.CountItems(); i++) { for (int32 i = 0; i < fProtocol->mailboxes.CountItems(); i++) {
work_path = path; work_path = path;
work_path.Append(_prot->mailboxes[i]); work_path.Append(fProtocol->mailboxes[i]);
node.SetTo(work_path.Path()); node.SetTo(work_path.Path());
node.GetNodeRef(&watcher); node.GetNodeRef(&watcher);
nodes[watcher.node] = strdup(_prot->mailboxes[i]); fNodes[watcher.node] = strdup(fProtocol->mailboxes[i]);
if (_prot->mailboxes[i][0] == 0) if (fProtocol->mailboxes[i][0] == 0) {
continue; //--- We've covered this in the parent monitor // We've covered this in the parent monitor
watch_node(&watcher,B_WATCH_DIRECTORY,this); continue;
_prot->SyncMailbox(_prot->mailboxes[i]); }
watch_node(&watcher, B_WATCH_DIRECTORY, this);
fProtocol->SyncMailbox(fProtocol->mailboxes[i]);
} }
((BMailChainRunner *)(Looper()))->ResetProgress(); ((BMailChainRunner *)(Looper()))->ResetProgress();
} break;
case B_NODE_MONITOR: {
int32 opcode;
if (msg->FindInt32("opcode",&opcode) < B_OK)
break; break;
int64 directory, node(msg->FindInt64("node"));
dev_t device(msg->FindInt32("device"));
bool is_dir;
{
node_ref item_ref;
item_ref.node = node;
item_ref.device = device;
BDirectory dir(&item_ref);
is_dir = (dir.InitCheck() == B_OK);
} }
if (opcode == B_ENTRY_MOVED) { case B_NODE_MONITOR: {
ino_t from, to; int32 opcode;
msg->FindInt64("from directory",&from); if (msg->FindInt32("opcode", &opcode) < B_OK)
msg->FindInt64("to directory",&to);
const char *from_mb(nodes[from]), *to_mb(nodes[to]);
if (to == dest_node)
to_mb = "";
if (from == dest_node)
from_mb = "";
if (from_mb == NULL) {
msg->AddInt64("directory",to);
opcode = B_ENTRY_CREATED;
} else if (to_mb == NULL) {
msg->AddInt64("directory",from);
if (is_dir)
opcode = B_ENTRY_REMOVED;
} else {
if (!is_dir) {
{
node_ref item_ref;
item_ref.node = to;
item_ref.device = device;
BDirectory dir(&item_ref);
BNode node(&dir,msg->FindString("name"));
//--- Why in HELL can't you make a BNode from a node_ref?
if (node.InitCheck() != B_OK)
break; //-- We're late, it's already gone elsewhere. Ignore for now.
BString id;
node.ReadAttrString("MAIL:unique_id",&id);
id.Truncate(id.FindLast('/'));
if (id == to_mb)
break; //-- Already where it belongs, no need to do anything
}
snooze(uint64(5e5));
_prot->SyncMailbox(to_mb);
//node.WriteAttrString("MAIL:unique_id",&id);
_prot->CheckForDeletedMessages();
} else {
BString mb;
if (to_mb[0] == 0)
mb = msg->FindString("name");
else {
mb = to_mb;
mb << '/' << msg->FindString("name");
}
if (strcmp(mb.String(),nodes[node]) == 0)
break;
if (_prot->CreateMailbox(mb.String()) < B_OK)
break;
_prot->mailboxes += mb.String();
_prot->SyncMailbox(mb.String());
_prot->CheckForDeletedMessages();
_prot->DeleteMailbox(nodes[node]);
_prot->mailboxes -= nodes[node];
free((void *)nodes[node]);
nodes[node] = strdup(mb.String());
}
break; break;
int64 directory, node(msg->FindInt64("node"));
dev_t device(msg->FindInt32("device"));
bool is_dir;
{
node_ref item_ref;
item_ref.node = node;
item_ref.device = device;
BDirectory dir(&item_ref);
is_dir = (dir.InitCheck() == B_OK);
} }
}
if (opcode == B_ENTRY_MOVED) {
msg->FindInt64("directory",&directory); ino_t from, to;
switch (opcode) { msg->FindInt64("from directory", &from);
case B_ENTRY_CREATED: msg->FindInt64("to directory", &to);
if (!is_dir) { const char *from_mb(fNodes[from]), *to_mb(fNodes[to]);
const char *dir = nodes[directory]; if (to == fDestinationNode)
snooze(500000); to_mb = "";
if (from == fDestinationNode)
if (dir == NULL) from_mb = "";
dir = ""; if (from_mb == NULL) {
msg->AddInt64("directory", to);
if (!_prot->mailboxes.HasItem(dir)) opcode = B_ENTRY_CREATED;
break; } else if (to_mb == NULL) {
msg->AddInt64("directory", from);
{ if (is_dir)
node_ref item_ref; opcode = B_ENTRY_REMOVED;
item_ref.node = directory;
item_ref.device = device;
BDirectory dir(&item_ref);
BNode node(&dir,msg->FindString("name"));
//--- Why in HELL can't you make a BNode from a node_ref?
if (node.InitCheck() != B_OK)
break; //-- We're late, it's already gone elsewhere. Ignore for now.
}
_prot->SyncMailbox(nodes[directory]);
} else { } else {
BString mb; if (!is_dir) {
if (directory == dest_node) {
mb = msg->FindString("name"); node_ref item_ref;
else { item_ref.node = to;
mb = nodes[directory]; item_ref.device = device;
mb << '/' << msg->FindString("name"); BDirectory dir(&item_ref);
BNode node(&dir,msg->FindString("name"));
// Why in HELL can't you make a BNode from a node_ref?
if (node.InitCheck() != B_OK) {
// We're late, it's already gone elsewhere. Ignore for now.
break;
}
BString id;
node.ReadAttrString("MAIL:unique_id", &id);
id.Truncate(id.FindLast('/'));
if (id == to_mb) {
// Already where it belongs, no need to do anything
break;
}
}
snooze(uint64(5e5));
fProtocol->SyncMailbox(to_mb);
//node.WriteAttrString("MAIL:unique_id",&id);
fProtocol->CheckForDeletedMessages();
} else {
BString mb;
if (to_mb[0] == 0)
mb = msg->FindString("name");
else {
mb = to_mb;
mb << '/' << msg->FindString("name");
}
if (strcmp(mb.String(), fNodes[node]) == 0)
break;
if (fProtocol->CreateMailbox(mb.String()) < B_OK)
break;
fProtocol->mailboxes += mb.String();
fProtocol->SyncMailbox(mb.String());
fProtocol->CheckForDeletedMessages();
fProtocol->DeleteMailbox(fNodes[node]);
fProtocol->mailboxes -= fNodes[node];
free((void *)fNodes[node]);
fNodes[node] = strdup(mb.String());
} }
if (_prot->CreateMailbox(mb.String()) < B_OK) break;
break;
nodes[node] = strdup(mb.String());
_prot->mailboxes += mb.String();
_prot->SyncMailbox(mb.String());
node_ref ref;
ref.device = device;
ref.node = node;
watch_node(&ref,B_WATCH_DIRECTORY,this);
} }
break; }
case B_ENTRY_REMOVED:
_prot->CheckForDeletedMessages(); msg->FindInt64("directory", &directory);
if ((is_dir) && (nodes[node] != NULL)) { switch (opcode) {
_prot->DeleteMailbox(nodes[node]); case B_ENTRY_CREATED:
_prot->mailboxes -= nodes[node]; if (!is_dir) {
free((void *)nodes[node]); const char *dir = fNodes[directory];
nodes[node] = NULL; snooze(500000);
node_ref ref; // half a second
ref.device = device;
ref.node = node; if (dir == NULL)
watch_node(&ref,B_STOP_WATCHING,this); dir = "";
}
break; if (!fProtocol->mailboxes.HasItem(dir))
break;
{
node_ref nodeRef;
nodeRef.node = directory;
nodeRef.device = device;
BDirectory dir(&nodeRef);
BNode node(&dir, msg->FindString("name"));
if (node.InitCheck() != B_OK)
break; //-- We're late, it's already gone elsewhere. Ignore for now.
}
fProtocol->SyncMailbox(fNodes[directory]);
} else {
BString mb;
if (directory == fDestinationNode)
mb = msg->FindString("name");
else {
mb = fNodes[directory];
mb << '/' << msg->FindString("name");
}
if (fProtocol->CreateMailbox(mb.String()) < B_OK)
break;
fNodes[node] = strdup(mb.String());
fProtocol->mailboxes += mb.String();
fProtocol->SyncMailbox(mb.String());
node_ref ref;
ref.device = device;
ref.node = node;
watch_node(&ref, B_WATCH_DIRECTORY, this);
}
break;
case B_ENTRY_REMOVED:
fProtocol->CheckForDeletedMessages();
if ((is_dir) && (fNodes[node] != NULL)) {
fProtocol->DeleteMailbox(fNodes[node]);
fProtocol->mailboxes -= fNodes[node];
free((void *)fNodes[node]);
fNodes[node] = NULL;
node_ref ref;
ref.device = device;
ref.node = node;
watch_node(&ref, B_STOP_WATCHING, this);
}
break;
}
} }
break;
} break;
} }
} }
private: private:
BRemoteMailStorageProtocol *_prot; BRemoteMailStorageProtocol *fProtocol;
BDirectory _dest; BDirectory fDestination;
map<int64, const char *> nodes; map<int64, const char *> fNodes;
ino_t dest_node; ino_t fDestinationNode;
}; };
void GetSubFolders(BDirectory *of, BStringList *folders, const char *prepend) {
void
GetSubFolders(BDirectory *of, BStringList *folders, const char *prepend)
{
of->Rewind(); of->Rewind();
BEntry ent;
BString crud; BEntry entry;
BDirectory sub; while (of->GetNextEntry(&entry) == B_OK) {
char buf[255]; if (!entry.IsDirectory())
while (of->GetNextEntry(&ent) == B_OK) { continue;
if (ent.IsDirectory()) {
sub.SetTo(&ent); BDirectory subDirectory(&entry);
ent.GetName(buf);
crud = prepend; char buffer[B_FILE_NAME_LENGTH];
crud << buf << '/'; entry.GetName(buffer);
GetSubFolders(&sub,folders,crud.String());
crud = prepend; BString path = prepend;
crud << buf; path << buffer << '/';
(*folders) += crud.String(); GetSubFolders(&subDirectory, folders, path.String());
} path = prepend;
path << buffer;
*folders += path.String();
} }
} }
BRemoteMailStorageProtocol::BRemoteMailStorageProtocol(BMessage *settings, BMailChainRunner *runner) : BMailProtocol(settings,runner) {
handler = new UpdateHandler(this,runner->Chain()->MetaData()->FindString("path")); BRemoteMailStorageProtocol::BRemoteMailStorageProtocol(BMessage *settings,
BMailChainRunner *runner)
: BMailProtocol(settings, runner)
{
handler = new UpdateHandler(this, runner->Chain()->MetaData()->FindString("path"));
runner->AddHandler(handler); runner->AddHandler(handler);
runner->PostMessage('INIT',handler); runner->PostMessage('INIT', handler);
} }
BRemoteMailStorageProtocol::~BRemoteMailStorageProtocol() {
BRemoteMailStorageProtocol::~BRemoteMailStorageProtocol()
{
delete handler; delete handler;
} }
}
} // empty namespace
//----BMailProtocol stuff //----BMailProtocol stuff
status_t BRemoteMailStorageProtocol::GetMessage(
const char* uid,
BPositionIO** out_file, BMessage* out_headers, status_t
BPath* out_folder_location) { BRemoteMailStorageProtocol::GetMessage(const char *uid,
BString folder(uid), id; BPositionIO **outFile, BMessage *outHeaders,
{ BPath *outFolderLocation)
BString raw(uid); {
folder.Truncate(raw.FindLast('/')); BString folder(uid), id;
raw.CopyInto(id,raw.FindLast('/') + 1,raw.Length()); {
} BString raw(uid);
folder.Truncate(raw.FindLast('/'));
*out_folder_location = folder.String(); raw.CopyInto(id, raw.FindLast('/') + 1, raw.Length());
return GetMessage(folder.String(),id.String(),out_file,out_headers);
} }
*outFolderLocation = folder.String();
return GetMessage(folder.String(), id.String(), outFile, outHeaders);
}
status_t BRemoteMailStorageProtocol::DeleteMessage(const char* uid) {
status_t
BRemoteMailStorageProtocol::DeleteMessage(const char *uid)
{
BString folder(uid), id; BString folder(uid), id;
{ {
BString raw(uid); BString raw(uid);
int32 j = raw.FindLast('/'); int32 j = raw.FindLast('/');
folder.Truncate(j); folder.Truncate(j);
raw.CopyInto(id,j + 1,raw.Length()); raw.CopyInto(id, j + 1, raw.Length());
} }
status_t err; status_t err;
if ((err = DeleteMessage(folder.String(),id.String())) < B_OK) if ((err = DeleteMessage(folder.String(), id.String())) < B_OK)
return err; return err;
(*unique_ids) -= uid; *unique_ids -= uid;
return B_OK; return B_OK;
} }
void BRemoteMailStorageProtocol::SyncMailbox(const char *mailbox) {
void
BRemoteMailStorageProtocol::SyncMailbox(const char *mailbox)
{
BPath path(runner->Chain()->MetaData()->FindString("path")); BPath path(runner->Chain()->MetaData()->FindString("path"));
path.Append(mailbox); path.Append(mailbox);
BDirectory folder(path.Path()); BDirectory folder(path.Path());
BEntry entry; BEntry entry;
BFile snoodle;
BString string; BString string;
uint32 chain; uint32 chain;
bool append; bool append;
@ -324,18 +361,23 @@ void BRemoteMailStorageProtocol::SyncMailbox(const char *mailbox) {
while (folder.GetNextEntry(&entry) == B_OK) { while (folder.GetNextEntry(&entry) == B_OK) {
if (!entry.IsFile()) if (!entry.IsFile())
continue; continue;
while (snoodle.SetTo(&entry,B_READ_WRITE) == B_BUSY) snooze(100);
BFile file;
while (file.SetTo(&entry, B_READ_WRITE) == B_BUSY)
snooze(100);
append = false; append = false;
while (snoodle.Lock() != B_OK) snooze(100); while (file.Lock() != B_OK)
snoodle.Unlock(); snooze(100);
file.Unlock();
if (snoodle.ReadAttr("MAIL:chain",B_INT32_TYPE,0,&chain,sizeof(chain)) < B_OK)
if (file.ReadAttr("MAIL:chain", B_INT32_TYPE, 0, &chain, sizeof(chain)) < B_OK)
append = true; append = true;
if (chain != runner->Chain()->ID()) { if (chain != runner->Chain()->ID()) {
uint32 pendingChain(~0UL), flags(0); uint32 pendingChain(~0UL), flags(0);
snoodle.ReadAttr("MAIL:pending_chain",B_INT32_TYPE,0,&pendingChain,sizeof(chain)); file.ReadAttr("MAIL:pending_chain", B_INT32_TYPE, 0, &pendingChain, sizeof(chain));
snoodle.ReadAttr("MAIL:flags",B_INT32_TYPE,0,&flags,sizeof(flags)); file.ReadAttr("MAIL:flags", B_INT32_TYPE, 0, &flags, sizeof(flags));
if (pendingChain == runner->Chain()->ID() if (pendingChain == runner->Chain()->ID()
&& BMailChain(chain).ChainDirection() == outbound && BMailChain(chain).ChainDirection() == outbound
@ -343,61 +385,51 @@ void BRemoteMailStorageProtocol::SyncMailbox(const char *mailbox) {
// Ignore this message, recode the chain attribute at the next SyncMailbox() // Ignore this message, recode the chain attribute at the next SyncMailbox()
continue; continue;
} }
if (pendingChain == runner->Chain()->ID()) { if (pendingChain == runner->Chain()->ID()) {
chain = runner->Chain()->ID(); chain = runner->Chain()->ID();
snoodle.WriteAttr("MAIL:chain",B_INT32_TYPE,0,&chain,sizeof(chain)); file.WriteAttr("MAIL:chain", B_INT32_TYPE, 0, &chain, sizeof(chain));
append = false; append = false;
} else } else
append = true; append = true;
} }
if (snoodle.ReadAttrString("MAIL:unique_id",&string) < B_OK) if (file.ReadAttrString("MAIL:unique_id", &string) < B_OK)
append = true; append = true;
BString folder(string), id(""); BString folder(string), id("");
int32 j = string.FindLast('/'); int32 j = string.FindLast('/');
if ((!append) && (j >= 0)) { if (!append && j >= 0) {
folder.Truncate(j); folder.Truncate(j);
string.CopyInto(id,j + 1,string.Length()); string.CopyInto(id, j + 1, string.Length());
if (folder == mailbox) if (folder == mailbox)
continue; continue;
} else { } else
append = true; append = true;
}
if (append) {
if (append) // ToDo: We should check for partial messages here
AddMessage(mailbox,&snoodle,&id); //---We should check for partial messages here AddMessage(mailbox, &file, &id);
else } else
CopyMessage(folder.String(),mailbox,&id); CopyMessage(folder.String(), mailbox, &id);
string = mailbox; string = mailbox;
string << '/' << id; string << '/' << id;
/*snoodle.RemoveAttr("MAIL:unique_id"); /*file.RemoveAttr("MAIL:unique_id");
snoodle.RemoveAttr("MAIL:chain");*/ file.RemoveAttr("MAIL:chain");*/
chain = runner->Chain()->ID(); chain = runner->Chain()->ID();
int32 flags = 0; int32 flags = 0;
snoodle.ReadAttr("MAIL:flags",B_INT32_TYPE,0,&flags,sizeof(flags)); file.ReadAttr("MAIL:flags", B_INT32_TYPE, 0, &flags, sizeof(flags));
if (flags & B_MAIL_PENDING) if (flags & B_MAIL_PENDING)
snoodle.WriteAttr("MAIL:pending_chain",B_INT32_TYPE,0,&chain,sizeof(chain)); file.WriteAttr("MAIL:pending_chain", B_INT32_TYPE, 0, &chain, sizeof(chain));
else else
snoodle.WriteAttr("MAIL:chain",B_INT32_TYPE,0,&chain,sizeof(chain)); file.WriteAttr("MAIL:chain", B_INT32_TYPE, 0, &chain, sizeof(chain));
snoodle.WriteAttrString("MAIL:unique_id",&string); file.WriteAttrString("MAIL:unique_id", &string);
(*manifest) += string.String(); *manifest += string.String();
(*unique_ids) += string.String(); *unique_ids += string.String();
string = runner->Chain()->Name(); string = runner->Chain()->Name();
snoodle.WriteAttrString("MAIL:account",&string); file.WriteAttrString("MAIL:account", &string);
} }
} }
/*status_t BRemoteMailStorageProtocol::MoveMessage(const char *mailbox, const char *to_mailbox, BString *message) {
BString new_id(*message);
status_t err;
if ((err = CopyMessage(mailbox,to_mailbox,&new_id)) < B_OK)
return err;
if ((err = DeleteMessage(mailbox,message->String())) < B_OK)
return err;
*message = new_id;
return B_OK;
}*/