void SaveCompCmd::Execute () { Editor* ed = GetEditor(); ModifStatusVar* modifVar = (ModifStatusVar*)ed->GetState("ModifStatusVar"); CompNameVar* compNameVar = (CompNameVar*) ed->GetState("CompNameVar"); const char* name = (compNameVar == nil) ? nil : compNameVar->GetName(); if (name == nil) { SaveCompAsCmd saveCompAs(ed); saveCompAs.Execute(); } else if (modifVar == nil || modifVar->GetModifStatus()) { Catalog* catalog = unidraw->GetCatalog(); Component* comp; if (catalog->Retrieve(name, comp) && catalog->Save(comp, name)) { if (modifVar != nil) modifVar->SetModifStatus(false); unidraw->ClearHistory(comp); } else { char title[CHARBUFSIZE]; const char* reason = !Writable(comp) ? "(File not writable.)" : ""; sprintf(title, "Couldn't save! %s", reason); char subtitle[CHARBUFSIZE]; const char* domain = unidraw->GetCatalog()->GetAttribute("domain"); domain = (domain == nil) ? "component" : domain; sprintf(subtitle, "Save this %s as:", domain); Style* s = new Style(Session::instance()->style()); s->attribute("caption", title); s->attribute("subcaption", subtitle); s->attribute("open", "Save"); /* BUG: style s is never used!!!! */ SaveCompAsCmd saveCompAs(ed); saveCompAs.Execute(); } } }
void SaveCompAsCmd::Execute () { Editor* ed = GetEditor(); char buf[CHARBUFSIZE]; const char* domain = unidraw->GetCatalog()->GetAttribute("domain"); domain = (domain == nil) ? "component" : domain; sprintf(buf, "Save this %s as:", domain); boolean reset_caption = false; Style* style = new Style(Session::instance()->style()); style->attribute("subcaption", buf); style->attribute("open", "Save"); if (chooser_ == nil) { chooser_ = DialogKit::instance()->file_chooser(".", style); Resource::ref(chooser_); } while (chooser_->post_for(ed->GetWindow())) { const String* str = chooser_->selected(); NullTerminatedString ns(*str); const char* name = ns.string(); Catalog* catalog = unidraw->GetCatalog(); boolean ok = true; if (catalog->Exists(name) && catalog->Writable(name)) { char buf[CHARBUFSIZE]; sprintf(buf, "\"%s\" already exists.", name); ConfirmDialog dialog(buf, "Overwrite?"); ed->InsertDialog(&dialog); char confirmation = dialog.Confirm(); ed->RemoveDialog(&dialog); if (confirmation == 'n') { ok = false; } else if (confirmation != 'y') { break; } } if (ok) { CompNameVar* cnv = (CompNameVar*) ed->GetState("CompNameVar"); const char* oldname = (cnv == nil) ? nil : cnv->GetName(); Component* comp = ed->GetComponent(); if (catalog->Exists(name) && !catalog->Writable(name)) { style->attribute( "caption", "Couldn't save! (File not writable.)" ); } else { if (oldname == nil) { comp = comp->GetRoot(); } else { catalog->Retrieve(oldname, comp); catalog->Forget(comp); } StateVar* sv = ed->GetState("ModifStatusVar"); ModifStatusVar* mv = (ModifStatusVar*) sv; if (catalog->Save(comp, name)) { if (mv != nil) mv->SetModifStatus(false); unidraw->ClearHistory(comp); UpdateCompNameVars(); break; } else { if (mv != nil) mv->Notify(); UpdateCompNameVars(); style->attribute("caption", "Couldn't save!"); reset_caption = true; } } } } if (reset_caption) { style->attribute("caption", ""); } }