diff --git a/uppsrc/ide/SelectPkg.cpp b/uppsrc/ide/SelectPkg.cpp index 26b7c4aae..b07b90626 100644 --- a/uppsrc/ide/SelectPkg.cpp +++ b/uppsrc/ide/SelectPkg.cpp @@ -13,29 +13,21 @@ void SelectPackageDlg::PackageMenu(Bar& menu) menu.Separator(); menu.Add(b, "Duplicate package..", [=] { RenamePackage(true); }); menu.Add(b, "Rename package..", [=] { RenamePackage(false); }); + menu.Add(b, "Copy package to..", [=] { MovePackage(true); }); + menu.Add(b, "Move package to..", [=] { MovePackage(false); }); menu.Add(b, "Delete package", THISBACK(DeletePackage)); } -bool RenamePackageFs(const String& upp, const String& newname, bool duplicate) +bool RenamePackageFs(const String& upp, const String& npf, const String& nupp, bool copy) { - if(IsNull(newname)) { - Exclamation("Wrong name."); - return false; - } String pf = GetFileFolder(upp); - String npf = AppendFileName(GetPackagePathNest(pf), newname); - String nupp = npf + "/" + GetFileName(newname) + ".upp"; - if(FileExists(nupp)) { - Exclamation("Package [* \1" + newname + "\1] already exists!"); - return false; - } String temp_pf = AppendFileName(GetFileFolder(pf), AsString(Random()) + AsString(Random())); if(!FileMove(pf, temp_pf)) { Exclamation("Operation has failed."); return false; } RealizePath(GetFileFolder(npf)); - if(duplicate) { + if(copy) { bool b = CopyFolder(npf, temp_pf); FileMove(temp_pf, pf); if(!b) { @@ -59,6 +51,23 @@ bool RenamePackageFs(const String& upp, const String& newname, bool duplicate) return true; } +bool RenamePackageFs(const String& upp, const String& newname, bool duplicate) +{ + if(IsNull(newname)) { + Exclamation("Wrong name."); + return false; + } + String npf = AppendFileName(GetPackagePathNest(GetFileFolder(upp)), newname); + String nupp = npf + "/" + GetFileName(newname) + ".upp"; + + if(FileExists(nupp)) { + Exclamation("Package [* \1" + newname + "\1] already exists!"); + return false; + } + + return RenamePackageFs(upp, npf, nupp, duplicate); +} + void SelectPackageDlg::RenamePackage(bool duplicate) { String n = GetCurrentName(); @@ -72,6 +81,65 @@ again: Load(n); } +void SelectPackageDlg::MovePackage(bool copy) +{ + WithMoveCopyPackageLayout dlg; + CtrlLayoutOKCancel(dlg, copy ? "Copy package to" : "Move package to"); + + String d0; + for(int pass = 0; pass < 2; pass++) { + Index udir; + FindFile ff(ConfigFile("*.var")); + while(ff) { + if(int(GetFileTitle(ff.GetName()) != base.GetKey()) == pass) { + VectorMap var; + LoadVarFile(ff.GetPath(), var); + for(String d : Split(var.Get("UPP", ""), ';')) + if(DirectoryExists(d)) { + udir.FindAdd(d); + d0 = Nvl(d0, d); + } + } + ff.Next(); + } + + Vector sd = pick(udir.PickKeys()); + Sort(sd, [](const String& a, const String& b) { return ToUpper(a) < ToUpper(b); }); + for(String d : sd) + dlg.dir.AddList(d); + } + + dlg.dir <<= d0; + dlg.select.SetImage(CtrlImg::Dir()); + dlg.select << [&] { String d = SelectDirectory(); if(d.GetCount()) dlg.dir <<= d; }; + + dlg.name <<= GetCurrentName(); + +again: + if(dlg.Run() != IDOK) + return; + + String dir = ~dlg.dir; + if(!DirectoryExists(dir)) { + Exclamation("Invalid target directory!"); + goto again; + } + String pkg = AppendFileName(dir, ~~dlg.name); + if(DirectoryExists(pkg)) { + Exclamation("Target package directory already exists!"); + goto again; + } + if(FileExists(pkg)) { + Exclamation("Invalid target package directory - it is a file!"); + goto again; + } + + if(!RenamePackageFs(PackagePath(GetCurrentName()), pkg, pkg + "/" + GetFileName(~~dlg.name) + ".upp", copy)) + goto again; + + Load(~~dlg.name); +} + void SelectPackageDlg::DeletePackage() { String n = GetCurrentName(); diff --git a/uppsrc/ide/UppDlg.h b/uppsrc/ide/UppDlg.h index 8c68861cd..3ef7e4937 100644 --- a/uppsrc/ide/UppDlg.h +++ b/uppsrc/ide/UppDlg.h @@ -223,6 +223,7 @@ struct SelectPackageDlg : public WithSelectPackageLayout { void RenamePackage(bool duplicate); void DeletePackage(); void PackageMenu(Bar& bar); + void MovePackage(bool copy); enum { MAIN = 1, FIRST = 2 diff --git a/uppsrc/ide/ide.lay b/uppsrc/ide/ide.lay index 398d90c5d..99580b611 100644 --- a/uppsrc/ide/ide.lay +++ b/uppsrc/ide/ide.lay @@ -129,6 +129,16 @@ LAYOUT(UppLayout, 936, 576) ITEM(EditIntSpin, tabsize, RightPosZ(6, 50).TopPosZ(28, 19)) END_LAYOUT +LAYOUT(MoveCopyPackageLayout, 408, 80) + ITEM(Label, dv___0, SetLabel(t_("Target directory")).LeftPosZ(4, 88).TopPosZ(4, 19)) + ITEM(WithDropChoice, dir, NotNull(true).LeftPosZ(96, 284).TopPosZ(4, 19)) + ITEM(Button, select, LeftPosZ(380, 20).TopPosZ(4, 19)) + ITEM(Label, dv___3, SetLabel(t_("Target name")).LeftPosZ(4, 88).TopPosZ(32, 19)) + ITEM(EditString, name, NotNull(true).LeftPosZ(96, 64).TopPosZ(32, 19)) + ITEM(Button, ok, SetLabel(t_("OK")).RightPosZ(76, 64).BottomPosZ(6, 22)) + ITEM(Button, cancel, SetLabel(t_("Cancel")).RightPosZ(8, 64).BottomPosZ(6, 22)) +END_LAYOUT + LAYOUT(NewPackageLayout, 892, 560) ITEM(Label, dv___0, SetLabel(t_("Package name")).LeftPosZ(8, 80).TopPosZ(4, 19)) ITEM(EditString, package, LeftPosZ(88, 268).TopPosZ(4, 19))