diff options
author | Ian Thompson <quornian@googlemail.com> | 2008-06-26 22:28:33 +0400 |
---|---|---|
committer | Ian Thompson <quornian@googlemail.com> | 2008-06-26 22:28:33 +0400 |
commit | aee5e9561069628cc81bd230339043bee37a43e3 (patch) | |
tree | e0e3c87391469e962b3d9ffb8a8c20dc7a24a985 /source | |
parent | fc392040dd035dc03899896cc56d337a8e97e78f (diff) |
Modifying a file externally or deleting a file linked with a Blender Text object presents options for reloading, saving or separating the Text object from the external file (like the make local idea).
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 14 | ||||
-rw-r--r-- | source/blender/include/BIF_drawtext.h | 1 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_text_types.h | 6 | ||||
-rw-r--r-- | source/blender/src/drawtext.c | 93 |
4 files changed, 110 insertions, 4 deletions
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index fbb94289166..f414498c1b9 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -30,6 +30,8 @@ */ #include <string.h> /* strstr */ +#include <sys/types.h> +#include <sys/stat.h> #include "MEM_guardedalloc.h" @@ -210,11 +212,12 @@ static void cleanup_textline(TextLine * tl) int reopen_text(Text *text) { FILE *fp; - int i, llen, len; + int i, llen, len, res; unsigned char *buffer; TextLine *tmp; char sfile[FILE_MAXFILE]; char str[FILE_MAXDIR+FILE_MAXFILE]; + struct stat fst; if (!text || !text->name) return 0; @@ -250,6 +253,9 @@ int reopen_text(Text *text) fseek(fp, 0L, SEEK_SET); text->undo_pos= -1; + + res= fstat(fp->_file, &fst); + text->mtime= fst.st_mtime; buffer= MEM_mallocN(len, "text_buffer"); // under windows fread can return less then len bytes because @@ -308,12 +314,13 @@ int reopen_text(Text *text) Text *add_text(char *file) { FILE *fp; - int i, llen, len; + int i, llen, len, res; unsigned char *buffer; TextLine *tmp; Text *ta; char sfile[FILE_MAXFILE]; char str[FILE_MAXDIR+FILE_MAXFILE]; + struct stat fst; BLI_strncpy(str, file, FILE_MAXDIR+FILE_MAXFILE); if (G.scene) /* can be NULL (bg mode) */ @@ -339,6 +346,9 @@ Text *add_text(char *file) ta->name= MEM_mallocN(strlen(file)+1, "text_name"); strcpy(ta->name, file); + res= fstat(fp->_file, &fst); + ta->mtime= fst.st_mtime; + ta->undo_pos= -1; ta->undo_len= TXT_INIT_UNDO; ta->undo_buf= MEM_mallocN(ta->undo_len, "undo buf"); diff --git a/source/blender/include/BIF_drawtext.h b/source/blender/include/BIF_drawtext.h index a75e0f1bd86..19f9d50e774 100644 --- a/source/blender/include/BIF_drawtext.h +++ b/source/blender/include/BIF_drawtext.h @@ -38,6 +38,7 @@ void unlink_text(struct Text *text); void free_textspace(struct SpaceText *st); +int txt_file_modified(struct Text *text); void txt_write_file(struct Text *text); void add_text_fs(char *file); diff --git a/source/blender/makesdna/DNA_text_types.h b/source/blender/makesdna/DNA_text_types.h index d1859d22115..d6ec62e63c0 100644 --- a/source/blender/makesdna/DNA_text_types.h +++ b/source/blender/makesdna/DNA_text_types.h @@ -31,6 +31,8 @@ #ifndef DNA_TEXT_TYPES_H #define DNA_TEXT_TYPES_H +#include "time.h" + #include "DNA_listBase.h" #include "DNA_ID.h" @@ -46,7 +48,7 @@ typedef struct Text { ID id; char *name; - + int flags, nlines; ListBase lines; @@ -57,6 +59,8 @@ typedef struct Text { int undo_pos, undo_len; void *compiled; + + time_t mtime; } Text; diff --git a/source/blender/src/drawtext.c b/source/blender/src/drawtext.c index 5cececa40a7..cd8f9be18ed 100644 --- a/source/blender/src/drawtext.c +++ b/source/blender/src/drawtext.c @@ -31,6 +31,8 @@ #include <math.h> #include <string.h> #include <ctype.h> +#include <sys/types.h> +#include <sys/stat.h> #ifdef HAVE_CONFIG_H #include <config.h> @@ -105,6 +107,7 @@ static void get_suggest_prefix(Text *text); static void confirm_suggestion(Text *text); static void *last_txt_find_string= NULL; +static double last_check_time= 0; static BMF_Font *spacetext_get_font(SpaceText *st) { static BMF_Font *scr12= NULL; @@ -1192,6 +1195,36 @@ void free_textspace(SpaceText *st) st->text= NULL; } +/* returns 0 if file on disk is the same or Text is in memory only + returns 1 if file has been modified on disk since last local edit + returns 2 if file on disk has been deleted + -1 is returned if an error occurs +*/ +int txt_file_modified(Text *text) +{ + struct stat st; + int result; + + if (!text || !text->name) + return 0; + + if (!BLI_exists(text->name)) + return 2; + + result = stat(text->name, &st); + + if(result == -1) + return -1; + + if((st.st_mode & S_IFMT) != S_IFREG) + return -1; + + if (st.st_mtime > text->mtime) + return 1; + + return 0; +} + static void save_mem_text(char *str) { SpaceText *st= curarea->spacedata.first; @@ -1218,6 +1251,8 @@ void txt_write_file(Text *text) { FILE *fp; TextLine *tmp; + int res; + struct stat fst; /* Do we need to get a filename? */ if (text->flags & TXT_ISMEM) { @@ -1225,7 +1260,7 @@ void txt_write_file(Text *text) activate_fileselect(FILE_SPECIAL, "SAVE TEXT FILE", text->name, save_mem_text); else activate_fileselect(FILE_SPECIAL, "SAVE TEXT FILE", text->id.name+2, save_mem_text); - return; + return; } /* Should we ask to save over? */ @@ -1252,6 +1287,9 @@ void txt_write_file(Text *text) } fclose (fp); + + res= stat(text->name, &fst); + text->mtime= fst.st_mtime; if (text->flags & TXT_ISDIRTY) text->flags ^= TXT_ISDIRTY; } @@ -2193,6 +2231,59 @@ void winqreadtextspace(ScrArea *sa, void *spacedata, BWinEvent *evt) } } + if (last_check_time < PIL_check_seconds_timer() - 1.0) { + switch (txt_file_modified(text)) { + case 1: + /* Modified locally and externally, ahhh. Offer more possibilites. */ + if (text->flags & TXT_ISDIRTY) { + switch (pupmenu("External File Modified with Local Changes %t|Load external changes (overwrite local) %x0|Save local changes (overwrite external) %x1|Make text internal %x2")) { + case 0: + reopen_text(text); + if (st->showsyntax) get_format_string(st); + do_draw= 1; + break; + case 1: + txt_write_file(text); + do_draw= 1; + break; + case 2: + text->flags |= TXT_ISMEM | TXT_ISDIRTY | TXT_ISTMP; + MEM_freeN(text->name); + text->name= NULL; + do_draw= 1; + break; + } + } else { + switch (pupmenu("External File Modified %t|Reload from disk %x0|Make text internal %x1")) { + case 0: + reopen_text(text); + if (st->showsyntax) get_format_string(st); + do_draw= 1; + break; + case 1: + text->flags |= TXT_ISMEM | TXT_ISDIRTY | TXT_ISTMP; + MEM_freeN(text->name); + text->name= NULL; + do_draw= 1; + break; + } + } + break; + case 2: + switch (pupmenu("External File Deleted %t|Make text internal %x0")) { + case 0: + text->flags |= TXT_ISMEM | TXT_ISDIRTY | TXT_ISTMP; + MEM_freeN(text->name); + text->name= NULL; + do_draw= 1; + break; + } + break; + default: + last_check_time = PIL_check_seconds_timer(); + } + } + if (suggesting) { if (do_suggest == -1) { suggest_clear_text(); |