Very old todo: Packed file UI

- The "ID" buttons (for browse images, for example) now show a Pack icon, for packed
  Images. Using this button allows unpack.

- Pack and unpack operations now give a Info report on what happened.

- Not restored yet: option to set "AutoPack".
This commit is contained in:
Ton Roosendaal 2013-01-27 14:28:45 +00:00
parent f0339c642d
commit 1a97efb1ba
6 changed files with 164 additions and 6 deletions

@ -35,6 +35,7 @@
#define RET_OK 0
#define RET_ERROR 1
struct ID;
struct bSound;
struct Image;
struct Main;
@ -72,5 +73,10 @@ int seekPackedFile(struct PackedFile *pf, int offset, int whence);
void rewindPackedFile(struct PackedFile *pf);
int readPackedFile(struct PackedFile *pf, void *data, int size);
/* ID should be not NULL, return 1 if there's a packed file */
int BKE_pack_check(struct ID *id);
/* ID should be not NULL, throws error when ID is Library */
void BKE_unpack_id(struct Main *bmain, struct ID *id, struct ReportList *reports, int how);
#endif

@ -44,9 +44,9 @@
#include "DNA_image_types.h"
#include "DNA_ID.h"
#include "DNA_packedFile_types.h"
#include "DNA_sound_types.h"
#include "DNA_vfont_types.h"
#include "DNA_packedFile_types.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
@ -233,11 +233,13 @@ void packAll(Main *bmain, ReportList *reports)
Image *ima;
VFont *vfont;
bSound *sound;
int tot = 0;
for (ima = bmain->image.first; ima; ima = ima->id.next) {
if (ima->packedfile == NULL && ima->id.lib == NULL) {
if (ima->source == IMA_SRC_FILE) {
ima->packedfile = newPackedFile(reports, ima->name, ID_BLEND_PATH(bmain, &ima->id));
tot ++;
}
else if (ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
BKE_reportf(reports, RPT_WARNING, "Image '%s' skipped, movies and image sequences not supported",
@ -246,13 +248,26 @@ void packAll(Main *bmain, ReportList *reports)
}
}
for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next)
if (vfont->packedfile == NULL && vfont->id.lib == NULL && BKE_vfont_is_builtin(vfont) == FALSE)
for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) {
if (vfont->packedfile == NULL && vfont->id.lib == NULL && BKE_vfont_is_builtin(vfont) == FALSE) {
vfont->packedfile = newPackedFile(reports, vfont->name, bmain->name);
tot ++;
}
}
for (sound = bmain->sound.first; sound; sound = sound->id.next)
if (sound->packedfile == NULL && sound->id.lib == NULL)
for (sound = bmain->sound.first; sound; sound = sound->id.next) {
if (sound->packedfile == NULL && sound->id.lib == NULL) {
sound->packedfile = newPackedFile(reports, sound->name, bmain->name);
tot++;
}
}
if (tot == 0)
BKE_report(reports, RPT_INFO, "No files have been packed");
else
BKE_reportf(reports, RPT_INFO, "Packed %d files", tot);
}
@ -316,6 +331,9 @@ int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, i
BKE_reportf(reports, RPT_ERROR, "Error writing file '%s'", name);
ret_value = RET_ERROR;
}
else
BKE_reportf(reports, RPT_INFO, "Saved packed file to: %s", name);
close(file);
}
else {
@ -439,6 +457,7 @@ char *unpackFile(ReportList *reports, const char *abs_name, const char *local_na
case PF_USE_ORIGINAL:
/* if file exists use it */
if (BLI_exists(abs_name)) {
BKE_reportf(reports, RPT_INFO, "Use existing file (instead of packed): %s", abs_name);
temp = abs_name;
break;
}
@ -604,3 +623,48 @@ void unpackAll(Main *bmain, ReportList *reports, int how)
unpackSound(bmain, reports, sound, how);
}
/* ID should be not NULL, return 1 if there's a packed file */
int BKE_pack_check(ID *id)
{
if (GS(id->name) == ID_IM) {
Image *ima = (Image *)id;
return ima->packedfile != NULL;
}
if (GS(id->name) == ID_VF) {
VFont *vf = (VFont *)id;
return vf->packedfile != NULL;
}
if (GS(id->name) == ID_SO) {
bSound *snd = (bSound *)id;
return snd->packedfile != NULL;
}
if (GS(id->name) == ID_LI) {
Library *li = (Library *)id;
return li->packedfile != NULL;
}
return 0;
}
/* ID should be not NULL */
void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
{
if (GS(id->name) == ID_IM) {
Image *ima = (Image *)id;
if (ima->packedfile)
unpackImage(reports, ima, how);
}
if (GS(id->name) == ID_VF) {
VFont *vf = (VFont *)id;
if (vf->packedfile)
unpackVFont(reports, vf, how);
}
if (GS(id->name) == ID_SO) {
bSound *snd = (bSound *)id;
if (snd->packedfile)
unpackSound(bmain, reports, snd, how);
}
if (GS(id->name) == ID_LI) {
Library *li = (Library *)id;
BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->name);
}
}

@ -58,6 +58,7 @@
#include "BKE_material.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_packedFile.h"
#include "BKE_particle.h"
#include "BKE_report.h"
#include "BKE_sca.h"
@ -536,7 +537,18 @@ static void template_ID(bContext *C, uiLayout *layout, TemplateID *template, Str
uiButSetFlag(but, UI_BUT_DISABLED);
}
if (flag & UI_ID_OPEN) {
/* Due to space limit in UI - skip the "open" icon for packed data, and allow to unpack.
Only for images, sound and fonts */
if (id && BKE_pack_check(id)) {
but = uiDefIconButO(block, BUT, "FILE_OT_unpack_item", WM_OP_INVOKE_REGION_WIN, ICON_UGLYPACKAGE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "Packed File");
uiButGetOperatorPtrRNA(but);
RNA_string_set(but->opptr, "id_name", id->name+2);
RNA_int_set(but->opptr, "id_type", GS(id->name));
}
else if (flag & UI_ID_OPEN) {
int w = id ? UI_UNIT_X : (flag & UI_ID_ADD_NEW) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
if (openop) {

@ -39,6 +39,7 @@ struct ReportList;
void FILE_OT_pack_all(struct wmOperatorType *ot);
void FILE_OT_unpack_all(struct wmOperatorType *ot);
void FILE_OT_unpack_item(struct wmOperatorType *ot);
void FILE_OT_pack_libraries(struct wmOperatorType *ot);
void FILE_OT_unpack_libraries(struct wmOperatorType *ot);

@ -46,9 +46,11 @@
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_packedFile.h"
#include "BKE_report.h"
#include "BKE_screen.h"
#include "WM_api.h"
@ -249,6 +251,78 @@ void FILE_OT_unpack_all(wmOperatorType *ot)
RNA_def_enum(ot->srna, "method", unpack_all_method_items, PF_USE_LOCAL, "Method", "How to unpack");
}
/********************* unpack single item operator *********************/
static const EnumPropertyItem unpack_item_method_items[] = {
{PF_USE_LOCAL, "USE_LOCAL", 0, "Use file from current directory (create when necessary)", ""},
{PF_WRITE_LOCAL, "WRITE_LOCAL", 0, "Write file to current directory (overwrite existing file)", ""},
{PF_USE_ORIGINAL, "USE_ORIGINAL", 0, "Use file in original location (create when necessary)", ""},
{PF_WRITE_ORIGINAL, "WRITE_ORIGINAL", 0, "Write file to original location (overwrite existing file)", ""},
/* {PF_ASK, "ASK", 0, "Ask for each file", ""}, */
{0, NULL, 0, NULL, NULL}};
static int unpack_item_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
ID *id;
char idname[BKE_ST_MAXNAME];
int type = RNA_int_get(op->ptr, "id_type");
int method = RNA_enum_get(op->ptr, "method");
RNA_string_get(op->ptr, "id_name", idname);
id = BKE_libblock_find_name(type, idname);
if (id == NULL) {
BKE_report(op->reports, RPT_WARNING, "No packed file");
return OPERATOR_CANCELLED;
}
if (method != PF_KEEP)
BKE_unpack_id(bmain, id, op->reports, method); /* XXX PF_ASK can't work here */
G.fileflags &= ~G_AUTOPACK;
return OPERATOR_FINISHED;
}
static int unpack_item_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
uiPopupMenu *pup;
uiLayout *layout;
pup = uiPupMenuBegin(C, "Unpack", ICON_NONE);
layout = uiPupMenuLayout(pup);
uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
uiItemsFullEnumO(layout, op->type->idname, "method", op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
uiPupMenuEnd(C, pup);
return OPERATOR_CANCELLED;
}
void FILE_OT_unpack_item(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Unpack Item";
ot->idname = "FILE_OT_unpack_item";
ot->description = "Unpack this file to an external file";
/* api callbacks */
ot->exec = unpack_item_exec;
ot->invoke = unpack_item_invoke;
/* flags */
ot->flag = OPTYPE_UNDO;
/* properties */
RNA_def_enum(ot->srna, "method", unpack_item_method_items, PF_USE_LOCAL, "Method", "How to unpack");
RNA_def_string(ot->srna, "id_name", "", BKE_ST_MAXNAME, "ID name", "Name of ID block to unpack");
RNA_def_int(ot->srna, "id_type", 0, 0, INT_MAX, "ID Type", "Identifier type of ID block", 0, INT_MAX);
}
/********************* make paths relative operator *********************/
static int make_paths_relative_exec(bContext *C, wmOperator *op)

@ -180,6 +180,7 @@ static void info_operatortypes(void)
WM_operatortype_append(FILE_OT_pack_all);
WM_operatortype_append(FILE_OT_pack_libraries);
WM_operatortype_append(FILE_OT_unpack_all);
WM_operatortype_append(FILE_OT_unpack_item);
WM_operatortype_append(FILE_OT_unpack_libraries);
WM_operatortype_append(FILE_OT_make_paths_relative);