status_t NotificationView::LoadAppUsage(void) { SettingsFile settings("appsettings", "BeClan/InfoPopper", B_USER_SETTINGS_DIRECTORY); status_t error = settings.InitCheck(); if (settings.InitCheck() != B_OK) return B_ERROR; error = settings.Load(); if (error != B_OK) return B_ERROR; appview_t::iterator avIt; appusage_t::iterator auIt; for (avIt = fAppView.begin(); avIt != fAppView.end(); avIt++) { avIt->second->RemoveSelf(); delete avIt->second; }; fAppView.clear(); fCurrentAppView = NULL; for (auIt = fAppUsage.begin(); auIt != fAppUsage.end(); auIt++) delete auIt->second; fAppUsage.clear(); type_code type; int32 count = 0; error = settings.GetInfo("app_usage", &type, &count); if (error != B_OK) return B_ERROR; for (int32 i = 0; i < count; i++) { AppUsage *app = new AppUsage(); settings.FindFlat("app_usage", i, app); fAppUsage[app->Name()] = app; }; return B_OK; };
status_t NotificationsView::Load(BMessage& settings) { type_code type; int32 count = 0; if (settings.GetInfo("app_usage", &type, &count) != B_OK) return B_ERROR; // Clean filters appusage_t::iterator auIt; for (auIt = fAppFilters.begin(); auIt != fAppFilters.end(); auIt++) delete auIt->second; fAppFilters.clear(); // Add new filters for (int32 i = 0; i < count; i++) { AppUsage* app = new AppUsage(); settings.FindFlat("app_usage", i, app); fAppFilters[app->Name()] = app; } // Load the applications list _PopulateApplications(); return B_OK; }
void NotificationWindow::_LoadAppFilters(BMessage& settings) { type_code type; int32 count = 0; if (settings.GetInfo("app_usage", &type, &count) != B_OK) return; for (int32 i = 0; i < count; i++) { AppUsage* app = new AppUsage(); settings.FindFlat("app_usage", i, app); fAppFilters[app->Name()] = app; } }
void NotificationWindow::LoadAppFilters(bool startMonitor) { BPath path; if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) return; path.Append(kSettingsDirectory); if (create_directory(path.Path(), 0755) != B_OK) return; path.Append(kFiltersSettings); BFile file(path.Path(), B_READ_ONLY); BMessage settings; if (settings.Unflatten(&file) != B_OK) return; type_code type; int32 count = 0; if (settings.GetInfo("app_usage", &type, &count) != B_OK) return; for (int32 i = 0; i < count; i++) { AppUsage* app = new AppUsage(); settings.FindFlat("app_usage", i, app); fAppFilters[app->Name()] = app; } if (startMonitor) { node_ref nref; BEntry entry(path.Path()); entry.GetNodeRef(&nref); if (watch_node(&nref, B_WATCH_ALL, BMessenger(this)) != B_OK) { BAlert* alert = new BAlert(B_TRANSLATE("Warning"), B_TRANSLATE("Couldn't start filter monitor." " Live filter changes disabled."), B_TRANSLATE("Darn.")); alert->Go(); } } }
void NotificationWindow::MessageReceived(BMessage* message) { switch (message->what) { case B_NODE_MONITOR: { LoadSettings(); LoadAppFilters(); break; } case kResizeToFit: ResizeAll(); break; case B_COUNT_PROPERTIES: { BMessage reply(B_REPLY); BMessage specifier; const char* property = NULL; bool messageOkay = true; if (message->FindMessage("specifiers", 0, &specifier) != B_OK) messageOkay = false; if (specifier.FindString("property", &property) != B_OK) messageOkay = false; if (strcmp(property, "message") != 0) messageOkay = false; if (messageOkay) reply.AddInt32("result", fViews.size()); else { reply.what = B_MESSAGE_NOT_UNDERSTOOD; reply.AddInt32("error", B_ERROR); } message->SendReply(&reply); break; } case B_CREATE_PROPERTY: case kNotificationMessage: { int32 type; const char* content = NULL; const char* title = NULL; const char* app = NULL; BMessage reply(B_REPLY); bool messageOkay = true; if (message->FindInt32("type", &type) != B_OK) type = B_INFORMATION_NOTIFICATION; if (message->FindString("content", &content) != B_OK) messageOkay = false; if (message->FindString("title", &title) != B_OK) messageOkay = false; if (message->FindString("app", &app) != B_OK && message->FindString("appTitle", &app) != B_OK) messageOkay = false; if (messageOkay) { NotificationView* view = new NotificationView(this, (notification_type)type, app, title, content, new BMessage(*message)); appfilter_t::iterator fIt = fAppFilters.find(app); bool allow = false; if (fIt == fAppFilters.end()) { app_info info; BMessenger messenger = message->ReturnAddress(); if (messenger.IsValid()) be_roster->GetRunningAppInfo(messenger.Team(), &info); else be_roster->GetAppInfo("application/x-vnd.Be-SHEL", &info); AppUsage* appUsage = new AppUsage(info.ref, app, true); fAppFilters[app] = appUsage; appUsage->Allowed(title, (notification_type)type); allow = true; } else allow = fIt->second->Allowed(title, (notification_type)type); if (allow) { appview_t::iterator aIt = fAppViews.find(app); AppGroupView* group = NULL; if (aIt == fAppViews.end()) { group = new AppGroupView(this, app); fAppViews[app] = group; fBorder->AddChild(group); } else group = aIt->second; group->AddInfo(view); ResizeAll(); reply.AddInt32("error", B_OK); } else reply.AddInt32("error", B_NOT_ALLOWED); } else { reply.what = B_MESSAGE_NOT_UNDERSTOOD; reply.AddInt32("error", B_ERROR); } message->SendReply(&reply); break; } case kRemoveView: { void* _ptr; message->FindPointer("view", &_ptr); NotificationView* info = reinterpret_cast<NotificationView*>(_ptr); fBorder->RemoveChild(info); std::vector<NotificationView*>::iterator i = find(fViews.begin(), fViews.end(), info); if (i != fViews.end()) fViews.erase(i); delete info; ResizeAll(); break; } default: BWindow::MessageReceived(message); } }
void NotificationWindow::MessageReceived(BMessage* message) { switch (message->what) { case B_NODE_MONITOR: { _LoadSettings(); _LoadAppFilters(); break; } case B_COUNT_PROPERTIES: { BMessage reply(B_REPLY); BMessage specifier; const char* property = NULL; bool messageOkay = true; if (message->FindMessage("specifiers", 0, &specifier) != B_OK) messageOkay = false; if (specifier.FindString("property", &property) != B_OK) messageOkay = false; if (strcmp(property, "message") != 0) messageOkay = false; if (messageOkay) reply.AddInt32("result", fViews.size()); else { reply.what = B_MESSAGE_NOT_UNDERSTOOD; reply.AddInt32("error", B_ERROR); } message->SendReply(&reply); break; } case B_CREATE_PROPERTY: case kNotificationMessage: { BMessage reply(B_REPLY); BNotification* notification = new BNotification(message); if (notification->InitCheck() == B_OK) { bigtime_t timeout; if (message->FindInt64("timeout", &timeout) != B_OK) timeout = -1; BMessenger messenger = message->ReturnAddress(); app_info info; if (messenger.IsValid()) be_roster->GetRunningAppInfo(messenger.Team(), &info); else be_roster->GetAppInfo("application/x-vnd.Be-SHEL", &info); NotificationView* view = new NotificationView(this, notification, timeout); bool allow = false; appfilter_t::iterator it = fAppFilters.find(info.signature); if (it == fAppFilters.end()) { AppUsage* appUsage = new AppUsage(notification->Group(), true); appUsage->Allowed(notification->Title(), notification->Type()); fAppFilters[info.signature] = appUsage; allow = true; } else { allow = it->second->Allowed(notification->Title(), notification->Type()); } if (allow) { BString groupName(notification->Group()); appview_t::iterator aIt = fAppViews.find(groupName); AppGroupView* group = NULL; if (aIt == fAppViews.end()) { group = new AppGroupView(this, groupName == "" ? NULL : groupName.String()); fAppViews[groupName] = group; GetLayout()->AddView(group); } else group = aIt->second; group->AddInfo(view); _ResizeAll(); reply.AddInt32("error", B_OK); } else reply.AddInt32("error", B_NOT_ALLOWED); } else { reply.what = B_MESSAGE_NOT_UNDERSTOOD; reply.AddInt32("error", B_ERROR); } message->SendReply(&reply); break; } case kRemoveView: { NotificationView* view = NULL; if (message->FindPointer("view", (void**)&view) != B_OK) return; views_t::iterator it = find(fViews.begin(), fViews.end(), view); if (it != fViews.end()) fViews.erase(it); _ResizeAll(); break; } default: BWindow::MessageReceived(message); } }
void NotificationWindow::MessageReceived(BMessage* message) { switch (message->what) { case B_NODE_MONITOR: { _LoadSettings(); break; } case kNotificationMessage: { if (!fShouldRun) break; BMessage reply(B_REPLY); BNotification* notification = new BNotification(message); if (notification->InitCheck() == B_OK) { bigtime_t timeout; if (message->FindInt64("timeout", &timeout) != B_OK) timeout = fTimeout; BString sourceSignature(notification->SourceSignature()); BString sourceName(notification->SourceName()); bool allow = false; appfilter_t::iterator it = fAppFilters .find(sourceSignature.String()); AppUsage* appUsage = NULL; if (it == fAppFilters.end()) { if (sourceSignature.Length() > 0 && sourceName.Length() > 0) { appUsage = new AppUsage(sourceName.String(), sourceSignature.String(), true); fAppFilters[sourceSignature.String()] = appUsage; // TODO save back to settings file } allow = true; } else { appUsage = it->second; allow = appUsage->Allowed(); } if (allow) { BString groupName(notification->Group()); appview_t::iterator aIt = fAppViews.find(groupName); AppGroupView* group = NULL; if (aIt == fAppViews.end()) { group = new AppGroupView(this, groupName == "" ? NULL : groupName.String()); fAppViews[groupName] = group; GetLayout()->AddView(group); } else group = aIt->second; NotificationView* view = new NotificationView(notification, timeout, fIconSize); group->AddInfo(view); _ShowHide(); reply.AddInt32("error", B_OK); } else reply.AddInt32("error", B_NOT_ALLOWED); } else { reply.what = B_MESSAGE_NOT_UNDERSTOOD; reply.AddInt32("error", B_ERROR); } message->SendReply(&reply); break; } case kRemoveGroupView: { AppGroupView* view = NULL; if (message->FindPointer("view", (void**)&view) != B_OK) return; // It's possible that between sending this message, and us receiving // it, the view has become used again, in which case we shouldn't // delete it. if (view->HasChildren()) return; // this shouldn't happen if (fAppViews.erase(view->Group()) < 1) break; view->RemoveSelf(); delete view; _ShowHide(); break; } default: BWindow::MessageReceived(message); } }