status_t SaveRules(const char *path, BObjectList<FilerRule> *ruleList) { BEntry entry("/boot/home/config/settings/FilerRules"); if (entry.Exists()) entry.Remove(); CppSQLite3DB db; db.open("/boot/home/config/settings/FilerRules"); // While we could use other means of obtaining table names, this table is also // used for maintaining the order of the rules, which must be preserved DBCommand(db,"create table RuleList (ruleid int primary key, name varchar);", "PrefsWindow::SaveRules"); BString command; for (int32 i = 0; i < ruleList->CountItems(); i++) { FilerRule *rule = ruleList->ItemAt(i); // Test table: // 0) Entry type (test vs action) // 1) type // 2) mode // 3) value // 4) attribute type (if Attribute test) // 5) attribute type public name (short description) // 6) attribute public name (if Attribute test) BString tablename(EscapeIllegalCharacters(rule->GetDescription())); command = "create table "; command << tablename << "(entrytype varchar, testtype varchar, testmode varchar, testvalue varchar, attrtype varchar, attrtypename varchar, attrpublicname varchar);"; DBCommand(db,command.String(), "PrefsWindow::SaveRules"); command = "insert into RuleList values("; command << i << ",'" << tablename << "');"; DBCommand(db,command.String(), "PrefsWindow::SaveRules"); for (int32 j = 0; j < rule->CountTests(); j++) { BMessage *test = rule->TestAt(j); if (!test) continue; BString name,mode,value,mimeType,typeName, attrType, attrName; test->FindString("name",&name); test->FindString("mode",&mode); test->FindString("value",&value); test->FindString("mimetype",&mimeType); test->FindString("typename",&typeName); test->FindString("attrtype",&attrType); test->FindString("attrname",&attrName); command = "insert into "; command << tablename << " values('test', '" << EscapeIllegalCharacters(name.String()) << "', '" << EscapeIllegalCharacters(mode.String()) << "', '" << EscapeIllegalCharacters(value.String()) << "', '" << EscapeIllegalCharacters(mimeType.String()) << "', '" << EscapeIllegalCharacters(typeName.String()) << "', '" << EscapeIllegalCharacters(attrName.String()) << "');"; DBCommand(db,command.String(),"PrefsWindow::SaveRules:save test"); } for (int32 j = 0; j < rule->CountActions(); j++) { BMessage *action = rule->ActionAt(j); if (!action) continue; BString name,value; action->FindString("name",&name); action->FindString("value",&value); command = "insert into "; command << tablename << " values('action', '" << EscapeIllegalCharacters(name.String()) << "', '" << "', '" << EscapeIllegalCharacters(value.String()) << "', '', '', '');"; DBCommand(db,command.String(),"PrefsWindow::SaveRules:save action"); } } db.close(); return B_OK; }
void CatBrowser::MessageReceived(BMessage *msg) { switch(msg->what) { case M_QUIT_APP: { be_app->PostMessage(B_QUIT_REQUESTED); break; } case M_SET_CATEGORY: { BMenuItem *marked = fCategories->FindMarked(); if(!marked) break; else fCategory = marked->Label(); fBack->SetEnabled(false); SetupQuery(); RunQuery(); if(fPageCount < 2) fNext->SetEnabled(false); else fNext->SetEnabled(true); break; } case M_RESULTS_BACK: { if(fCurrentPage > 0) { fCurrentPage--; RunQuery(); } if(fCurrentPage < fPageCount - 1) fNext->SetEnabled(true); if(fCurrentPage == 0) fBack->SetEnabled(false); break; } case M_RESULTS_NEXT: { if(fCurrentPage < fPageCount - 1) { fCurrentPage++; if(fCurrentPage >= fPageCount - 1) fNext->SetEnabled(false); RunQuery(); } if(!fBack->IsEnabled()) fBack->SetEnabled(true); break; } case M_RECATEGORIZE_RECIPE: { int32 number; BString oldcat, newcat; msg->FindInt32("number",&number); msg->FindString("oldcategory",&oldcat); msg->FindString("newcategory",&newcat); int32 index = 0, selection; DBCommand("BEGIN","CatBrowser:begin mass recat"); BeginViewTransaction(); do { selection = fList->CurrentSelection(index); index++; if(selection >=0) { RecipeItem *item = (RecipeItem*)fList->ItemAt(selection); if(item) { ChangeCategory(item->fNumber, item->fCategory.String(), newcat.String()); } } } while(selection >= 0); EndViewTransaction(); DBCommand("COMMIT","ChefView:end mass recat"); SetupQuery(); RunQuery(); break; } case M_SHOW_RECAT: { int32 selection, firstselection, count=0; firstselection = selection = fList->CurrentSelection(); if(selection < 0) break; count++; do { selection = fList->CurrentSelection(count); if(selection >= 0) count++; } while (selection >= 0); RecipeItem *item = (RecipeItem*)fList->ItemAt(firstselection); if(item) { BRect r(Frame().OffsetByCopy(20,20)); r.right = r.left + 300; r.bottom = r.top + 200; RecatWindow *win = new RecatWindow(r,BMessenger(this), (count>1) ? -1 : item->fNumber, item->fCategory.String()); win->AddToSubset(this); win->Show(); } break; } case M_DELETE_RECIPE: { int32 index = 0, selection; DBCommand("BEGIN","CatBrowser:begin mass delete"); BeginViewTransaction(); do { selection = fList->CurrentSelection(index); index++; if(selection >=0) { RecipeItem *item = (RecipeItem*)fList->ItemAt(selection); if(item) DeleteRecipe(item->fNumber, item->fCategory.String()); } } while(selection >= 0); EndViewTransaction(); DBCommand("COMMIT","ChefView:end mass recat"); SetupQuery(); RunQuery(); // refresh the display in the main window in case we deleted something // that would be in the results BMessage msg(M_FIND_RECIPE); fMessenger.SendMessage(&msg); break; } case M_SET_RECIPE: { if(fEditMode) { if(fList->CurrentSelection(1)>=0) break; } RecipeItem *item = (RecipeItem*)fList->ItemAt(fList->CurrentSelection()); if(item) { BMessage msg(M_LOOKUP_RECIPE); msg.AddInt32("number",item->fNumber); msg.AddString("category",item->fCategory); msg.AddString("name",item->fName); fMessenger.SendMessage(&msg); } break; } default: BWindow::MessageReceived(msg); } }