Beispiel #1
0
void
CookieWindow::_ShowCookiesForDomain(BString domain)
{
	BString label;
	label.SetToFormat(B_TRANSLATE("Cookies for %s"), domain.String());
	fHeaderView->SetText(label);

	// Empty the cookie list
	fCookies->Clear();

	// Populate the domain list
	BNetworkCookieJar::Iterator it = fCookieJar.GetIterator();

	const BNetworkCookie* cookie;
	/* FIXME A direct access to a domain would be needed in BNetworkCookieJar. */
	while ((cookie = it.Next()) != NULL) {
		if (cookie->Domain() == domain)
			break;
	}

	if (cookie == NULL)
		return;

	do {
		new CookieRow(fCookies, *cookie); // Attaches itself to the list
		cookie = it.Next();
	} while (cookie != NULL && cookie->Domain() == domain);
}
Beispiel #2
0
void
CookieTest::SimpleTest()
{
	BNetworkCookieJar jar;
	char buffer[256];
	BUrl url("http://www.chipchapin.com/WebTools/cookietest.php");

	time_t t = time(NULL) + 6400; // Cookies expire in 1h45
	struct tm* now = gmtime(&t);
	status_t result;

	// Add various cookies
	result = jar.AddCookie("testcookie1=present;", url);
	CPPUNIT_ASSERT(result == B_OK);

	strftime(buffer, sizeof(buffer),
		"testcookie2=present; expires=%A, %d-%b-%Y %H:%M:%S", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	strftime(buffer, sizeof(buffer),
		"testcookie3=present; expires=%A, %d-%b-%Y %H:%M:%S; path=/", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	t += 3200; // expire in 2h40
	now = gmtime(&t);
	strftime(buffer, sizeof(buffer),
		"testcookie4=present; path=/; domain=www.chipchapin.com; "
		"expires=%A, %d-%b-%Y %H:%M:%S", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	// Now check they were all properly added
	BNetworkCookieJar::Iterator it = jar.GetIterator();

	int count = 0;
	while (it.HasNext()) {
		count ++;
		it.Next();
	}

	CPPUNIT_ASSERT_EQUAL(4, count);
}
Beispiel #3
0
void
CookieWindow::_BuildDomainList()
{
	// Empty the domain list (TODO should we do this when hiding instead?)
	for (int i = fDomains->FullListCountItems() - 1; i >= 1; i--) {
		delete fDomains->FullListItemAt(i);
	}
	fDomains->MakeEmpty();

	// BOutlineListView does not handle parent = NULL in many methods, so let's
	// make sure everything always has a parent.
	DomainItem* rootItem = new DomainItem("", true);
	fDomains->AddItem(rootItem);

	// Populate the domain list
	BNetworkCookieJar::Iterator it = fCookieJar.GetIterator();

	const BNetworkCookie* cookie;
	while ((cookie = it.NextDomain()) != NULL) {
		_AddDomain(cookie->Domain(), false);
	}

	int i = 1;
	while (i < fDomains->FullListCountItems())
	{
		DomainItem* item = (DomainItem*)fDomains->FullListItemAt(i);
		// Detach items from the fake root
		item->SetOutlineLevel(item->OutlineLevel() - 1);
		i++;
	}
	fDomains->RemoveItem(rootItem);
	delete rootItem;

	i = 0;
	int firstNotEmpty = i;
	// Collapse empty items to keep the list short
	while (i < fDomains->FullListCountItems())
	{
		DomainItem* item = (DomainItem*)fDomains->FullListItemAt(i);
		if (item->fEmpty == true) {
			if (fDomains->CountItemsUnder(item, true) == 1) {
				// The item has no cookies, and only a single child. We can
				// remove it and move its child one level up in the tree.

				int count = fDomains->CountItemsUnder(item, false);
				int index = fDomains->FullListIndexOf(item) + 1;
				for (int j = 0; j < count; j++) {
					BListItem* child = fDomains->FullListItemAt(index + j);
					child->SetOutlineLevel(child->OutlineLevel() - 1);
				}

				fDomains->RemoveItem(item);
				delete item;

				// The moved child is at the same index the removed item was.
				// We continue the loop without incrementing i to process it.
				continue;
			} else {
				// The item has no cookies, but has multiple children. Mark it
				// as disabled so it is not selectable.
				item->SetEnabled(false);
				if (i == firstNotEmpty)
					firstNotEmpty++;
			}
		}

		i++;
	}

	fDomains->Select(firstNotEmpty);
}