ide: Move/Copy package

git-svn-id: svn://ultimatepp.org/upp/trunk@13677 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2019-11-04 17:44:34 +00:00
parent 73a08f9a6b
commit 1e0fc30f35
3 changed files with 91 additions and 12 deletions

View file

@ -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<TopWindow> dlg;
CtrlLayoutOKCancel(dlg, copy ? "Copy package to" : "Move package to");
String d0;
for(int pass = 0; pass < 2; pass++) {
Index<String> udir;
FindFile ff(ConfigFile("*.var"));
while(ff) {
if(int(GetFileTitle(ff.GetName()) != base.GetKey()) == pass) {
VectorMap<String, String> 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<String> 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();

View file

@ -223,6 +223,7 @@ struct SelectPackageDlg : public WithSelectPackageLayout<TopWindow> {
void RenamePackage(bool duplicate);
void DeletePackage();
void PackageMenu(Bar& bar);
void MovePackage(bool copy);
enum {
MAIN = 1, FIRST = 2

View file

@ -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<EditString>, 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))