static char *DIR_GetStringPref(const char *prefRoot, const char *prefLeaf, const char *defaultValue) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return nsnull; nsCString value; nsCAutoString prefLocation(prefRoot); prefLocation.Append('.'); prefLocation.Append(prefLeaf); if (NS_SUCCEEDED(pPref->GetCharPref(prefLocation.get(), getter_Copies(value)))) { /* unfortunately, there may be some prefs out there which look like this */ if (value.EqualsLiteral("(null)")) { if (defaultValue) value = defaultValue; else value.Truncate(); } if (value.IsEmpty()) { rv = pPref->GetCharPref(prefLocation.get(), getter_Copies(value)); } } else value = defaultValue; return ToNewCString(value); }
static nsresult DIR_GetServerPreferences(nsVoidArray** list) { nsresult err; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &err)); if (NS_FAILED(err)) return err; PRInt32 version = -1; nsVoidArray *newList = nsnull; /* Update the ldap list version and see if there are old prefs to migrate. */ err = pPref->GetIntPref(PREF_LDAP_VERSION_NAME, &version); NS_ENSURE_SUCCESS(err, err); /* Find the new-style "ldap_2.servers" tree in prefs */ err = dir_GetPrefs(&newList); if (version < kCurrentListVersion) { pPref->SetIntPref(PREF_LDAP_VERSION_NAME, kCurrentListVersion); } DIR_SortServersByPosition(newList); *list = newList; return err; }
static nsresult dir_GetPrefs(nsVoidArray **list) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return rv; (*list) = new nsVoidArray(); if (!(*list)) return NS_ERROR_OUT_OF_MEMORY; char **children; PRUint32 prefCount; rv = dir_GetChildList(NS_LITERAL_CSTRING(PREF_LDAP_SERVER_TREE_NAME "."), &prefCount, &children); if (NS_FAILED(rv)) return rv; /* TBD: Temporary code to read broken "ldap" preferences tree. * Remove line with if statement after M10. */ if (dir_UserId == 0) pPref->GetIntPref(PREF_LDAP_GLOBAL_TREE_NAME".user_id", &dir_UserId); for (PRUint32 i = 0; i < prefCount; ++i) { DIR_Server *server; server = (DIR_Server *)PR_Calloc(1, sizeof(DIR_Server)); if (server) { DIR_InitServer(server); server->prefName = strdup(children[i]); DIR_GetPrefsForOneServer(server); if (server->description && server->description[0] && ((server->dirType == PABDirectory || server->dirType == IMDirectory || server->dirType == MAPIDirectory || server->dirType == FixedQueryLDAPDirectory || // this one might go away server->dirType == LDAPDirectory))) { if (!dir_IsServerDeleted(server)) { (*list)->AppendElement(server); } else DIR_DeleteServer(server); } else { DIR_DeleteServer(server); } } } NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(prefCount, children); return NS_OK; }
static char *DIR_GetLocalizedStringPref (const char *prefRoot, const char *prefLeaf, const char *defaultValue) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return nsnull; nsCAutoString prefLocation(prefRoot); prefLocation.Append('.'); prefLocation.Append(prefLeaf); nsString wvalue; nsCOMPtr<nsIPrefLocalizedString> locStr; rv = pPref->GetComplexValue(prefLocation.get(), NS_GET_IID(nsIPrefLocalizedString), getter_AddRefs(locStr)); if (NS_SUCCEEDED(rv)) rv = locStr->ToString(getter_Copies(wvalue)); char *value = nsnull; if (!wvalue.IsEmpty()) { NS_ConvertUTF16toUTF8 utf8str(wvalue.get()); value = ToNewCString(utf8str); } else value = defaultValue ? strdup(defaultValue) : nsnull; return value; }
static nsresult SavePrefsFile() { nsresult rv; nsCOMPtr<nsIPrefService> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return rv; return pPref->SavePrefFile(nsnull); }
static void DIR_GetPrefsForOneServer(DIR_Server *server) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return; char *prefstring = server->prefName; // this call fills in tempstring with the position pref, and // we then check to see if it's locked. server->position = DIR_GetIntPref (prefstring, "position", kDefaultPosition); // For default address books, this will get the name from the chrome // file referenced, for other address books it'll just retrieve it from prefs // as normal. server->description = DIR_GetDescription(prefstring); server->dirType = (DirectoryType)DIR_GetIntPref (prefstring, "dirType", LDAPDirectory); server->fileName = DIR_GetStringPref (prefstring, "filename", ""); // if we don't have a file name try and get one if (!server->fileName || !*(server->fileName)) DIR_SetServerFileName (server); if (server->fileName && *server->fileName) DIR_ConvertServerFileName(server); // the string "s" is the default uri ( <scheme> + "://" + <filename> ) nsCString s; switch (server->dirType) { case PABDirectory: case MAPIDirectory: s.Append(kMDBDirectoryRoot); break; case IMDirectory: s.Append(kIMDirectoryRoot); break; default: #if defined(MOZ_LDAP_XPCOM) s.Append(kLDAPDirectoryRoot); #else s.Append(kAllDirectoryRoot); #endif break; } s.Append (server->fileName); server->uri = DIR_GetStringPref (prefstring, "uri", s.get ()); }
static char *DIR_GetDescription(const char *prefRoot) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return nsnull; nsCAutoString prefLocation(prefRoot); prefLocation.AppendLiteral(".description"); nsString wvalue; nsCOMPtr<nsIPrefLocalizedString> locStr; rv = pPref->GetComplexValue(prefLocation.get(), NS_GET_IID(nsIPrefLocalizedString), getter_AddRefs(locStr)); if (NS_SUCCEEDED(rv)) rv = locStr->ToString(getter_Copies(wvalue)); char *value = nsnull; if (!wvalue.IsEmpty()) { NS_ConvertUTF16toUTF8 utf8str(wvalue.get()); value = ToNewCString(utf8str); } else { // In TB 2 only some prefs had chrome:// URIs. We had code in place that would // only get the localized string pref for the particular address books that // were built-in. // Additionally, nsIPrefBranch::getComplexValue will only get a non-user-set, // non-locked pref value if it is a chrome:// URI and will get the string // value at that chrome URI. This breaks extensions/autoconfig that want to // set default pref values and allow users to change directory names. // // Now we have to support this, and so if for whatever reason we fail to get // the localized version, then we try and get the non-localized version // instead. If the string value is empty, then we'll just get the empty value // back here. rv = pPref->GetCharPref(prefLocation.get(), &value); if (NS_FAILED(rv)) value = nsnull; } return value; }
static void DIR_SetStringPref(const char *prefRoot, const char *prefLeaf, const char *value, const char *defaultValue) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return; nsCString defaultPref; nsCAutoString prefLocation(prefRoot); prefLocation.Append('.'); prefLocation.Append(prefLeaf); if (NS_SUCCEEDED(pPref->GetCharPref(prefLocation.get(), getter_Copies(defaultPref)))) { /* If there's a default pref, just set ours in and let libpref worry * about potential defaults in all.js */ if (value) /* added this check to make sure we have a value before we try to set it..*/ rv = pPref->SetCharPref (prefLocation.get(), value); else rv = pPref->ClearUserPref(prefLocation.get()); } else { /* If there's no default pref, look for a user pref, and only set our value in * if the user pref is different than one of them. */ nsCString userPref; if (NS_SUCCEEDED(pPref->GetCharPref (prefLocation.get(), getter_Copies(userPref)))) { if (value && (defaultValue ? PL_strcasecmp(value, defaultValue) : value != defaultValue)) rv = pPref->SetCharPref (prefLocation.get(), value); else rv = pPref->ClearUserPref(prefLocation.get()); } else { if (value && (defaultValue ? PL_strcasecmp(value, defaultValue) : value != defaultValue)) rv = pPref->SetCharPref (prefLocation.get(), value); } } NS_ASSERTION(NS_SUCCEEDED(rv), "Could not set pref in DIR_SetStringPref"); }
static PRInt32 DIR_GetIntPref(const char *prefRoot, const char *prefLeaf, PRInt32 defaultValue) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return defaultValue; PRInt32 value; nsCAutoString prefLocation(prefRoot); prefLocation.Append('.'); prefLocation.Append(prefLeaf); if (NS_FAILED(pPref->GetIntPref(prefLocation.get(), &value))) value = defaultValue; return value; }
static void DIR_SetIntPref(const char *prefRoot, const char *prefLeaf, PRInt32 value, PRInt32 defaultValue) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return; PRInt32 defaultPref; nsCAutoString prefLocation(prefRoot); prefLocation.Append('.'); prefLocation.Append(prefLeaf); if (NS_SUCCEEDED(pPref->GetIntPref(prefLocation.get(), &defaultPref))) { /* solve the problem where reordering user prefs must override default prefs */ rv = pPref->SetIntPref(prefLocation.get(), value); } else { PRInt32 userPref; if (NS_SUCCEEDED(pPref->GetIntPref(prefLocation.get(), &userPref))) { if (value != defaultValue) rv = pPref->SetIntPref(prefLocation.get(), value); else rv = pPref->ClearUserPref(prefLocation.get()); } else { if (value != defaultValue) rv = pPref->SetIntPref(prefLocation.get(), value); } } NS_ASSERTION(NS_SUCCEEDED(rv), "Could not set pref in DIR_SetIntPref"); }
static void DIR_SaveServerPreferences(nsVoidArray *wholeList) { if (wholeList) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { NS_WARNING("DIR_SaveServerPreferences: Failed to get the pref service\n"); return; } PRInt32 i; PRInt32 count = wholeList->Count(); DIR_Server *server; for (i = 0; i < count; i++) { server = (DIR_Server *) wholeList->ElementAt(i); if (server) DIR_SavePrefsForOneServer(server); } pPref->SetIntPref(PREF_LDAP_GLOBAL_TREE_NAME".user_id", dir_UserId); } }
/* Function for setting the position of a server. Can be used to append, * delete, or move a server in a server list. * * The third parameter specifies the new position the server is to occupy. * The resulting position may differ depending on the lock state of the * given server and other servers in the list. The following special values * are supported: * DIR_POS_APPEND - Appends the server to the end of the list. If the server * is already in the list, does nothing. * DIR_POS_DELETE - Deletes the given server from the list. Note that this * does not cause the server structure to be freed. * * Returns PR_TRUE if the server list was re-sorted. */ static PRBool DIR_SetServerPosition(nsVoidArray *wholeList, DIR_Server *server, PRInt32 position) { NS_ENSURE_ARG_POINTER(wholeList); PRInt32 i, count, num; PRBool resort = PR_FALSE; DIR_Server *s=nsnull; switch (position) { case DIR_POS_APPEND: /* Do nothing if the request is to append a server that is already * in the list. */ count = wholeList->Count(); for (i= 0; i < count; i++) { if ((s = (DIR_Server *)wholeList->ElementAt(i)) != nsnull) if (s == server) return PR_FALSE; } /* In general, if there are any servers already in the list, set the * position to the position of the last server plus one. If there * are none, set it to position 1. */ if (count > 0) { s = (DIR_Server *)wholeList->ElementAt(count - 1); server->position = s->position + 1; } else server->position = 1; wholeList->AppendElement(server); break; case DIR_POS_DELETE: /* Remove the prefs corresponding to the given server. If the prefName * value is nsnull, the server has never been saved and there are no * prefs to remove. */ if (server->prefName) { nsresult rv; nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) return PR_FALSE; pPref->DeleteBranch(server->prefName); // mark the server as deleted by setting its position to 0 DIR_SetIntPref(server->prefName, "position", 0, -1); } /* If the server is in the server list, remove it. */ num = wholeList->IndexOf(server); if (num >= 0) { /* The list does not need to be re-sorted if the server is the * last one in the list. */ count = wholeList->Count(); if (num == count - 1) { wholeList->RemoveElementAt(num); } else { resort = PR_TRUE; wholeList->RemoveElement(server); } } break; default: /* See if the server is already in the list. */ count = wholeList->Count(); for (i= 0; i < count; i++) { if ((s = (DIR_Server *)wholeList->ElementAt(i)) != nsnull) if (s == server) break; } /* If the server is not in the list, add it to the beginning and re-sort. */ if (s == nsnull) { server->position = position; wholeList->AppendElement(server); resort = PR_TRUE; } /* Don't re-sort if the server is already in the requested position. */ else if (server->position != position) { server->position = position; wholeList->RemoveElement(server); wholeList->AppendElement(server); resort = PR_TRUE; } break; } /* Make sure our position changes get saved back to prefs */ DIR_SaveServerPreferences(wholeList); return resort; }