Skip to content

Commit

Permalink
Query account names, and name root folders for the related account
Browse files Browse the repository at this point in the history
  • Loading branch information
smowton committed Apr 15, 2019
1 parent 81070c2 commit 5023acb
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions Source/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ struct im2_maildir_folder {
std::vector<struct im2_maildir_folder*> children;
std::string id;
std::string foldername;
std::string account;

im2_maildir_folder() : parent(0) {}

Expand All @@ -417,6 +418,26 @@ struct im2_maildir_folder {

}

std::string get_unique_account_name() const {
if (account.length() != 0)
return account;

std::string unique_account_name;
for (const auto child : children) {
std::string child_account = child->get_unique_account_name();
if (child_account.length() != 0) {
if (unique_account_name.length() == 0)
unique_account_name = child_account;
else if (unique_account_name != child_account) {
unique_account_name = "";
break;
}
}
}

return unique_account_name;
}

};

void WINAPI process_emails2(const char* im_database_filename, const char* im_attachments_directory);
Expand Down Expand Up @@ -571,16 +592,40 @@ enum INCREDIMAIL_VERSIONS incredimail_version;
thisfolder.id = thisid;
thisfolder.foldername = label;

// Figure out the owning account name from message headers:
std::string name_query = "select account, count(*) from headers where containerid = \"" + thisfolder.id + "\" group by account order by count(*) desc limit 1";
sqlite3_stmt *name_statement = NULL;

if (sqlite3_prepare_v2(db, name_query.c_str(), -1, &name_statement, NULL) != SQLITE_OK) {
sqlite3_close(db);
MessageBox(global_hwnd, "Account name query failed", "Error!", MB_OK);
return;
}

if (sqlite3_step(name_statement) == SQLITE_ROW) {
thisfolder.account = (const char *)sqlite3_column_text(name_statement, 0);
}

sqlite3_finalize(name_statement);

}

int rootidx = 0;
for (std::map<std::string, im2_maildir_folder>::iterator it = containers.begin(), itend = containers.end(); it != itend; ++it) {

if (it->second.parent)
continue;
char rootlabel[64];
sprintf_s(rootlabel, "root folder %d", ++rootidx);
it->second.foldername = rootlabel;

// Try to inherit an account name:
std::string unique_account_name = it->second.get_unique_account_name();

if (unique_account_name.length() != 0) {
it->second.foldername = "Account " + std::to_string(++rootidx) + " (" + unique_account_name + ")";
}
else {
it->second.foldername = "Unnamed account " + std::to_string(++rootidx);
}

if (!it->second.create_directories(export_directory, container_to_dir)) {
MessageBox(global_hwnd, "Failed to create some message directory", "Error!", MB_OK);
sqlite3_close(db);
Expand Down

0 comments on commit 5023acb

Please sign in to comment.