[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15364] branches/soc-2008-quorn/source/ blender: 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).

Ian Thompson quornian at googlemail.com
Thu Jun 26 20:28:39 CEST 2008


Revision: 15364
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15364
Author:   quorn
Date:     2008-06-26 20:28:33 +0200 (Thu, 26 Jun 2008)

Log Message:
-----------
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).

Modified Paths:
--------------
    branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c
    branches/soc-2008-quorn/source/blender/include/BIF_drawtext.h
    branches/soc-2008-quorn/source/blender/makesdna/DNA_text_types.h
    branches/soc-2008-quorn/source/blender/src/drawtext.c

Modified: branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c	2008-06-26 18:15:45 UTC (rev 15363)
+++ branches/soc-2008-quorn/source/blender/blenkernel/intern/text.c	2008-06-26 18:28:33 UTC (rev 15364)
@@ -30,6 +30,8 @@
  */
 
 #include <string.h> /* strstr */
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "MEM_guardedalloc.h"
 
@@ -210,11 +212,12 @@
 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 @@
 	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 @@
 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 @@
 	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");

Modified: branches/soc-2008-quorn/source/blender/include/BIF_drawtext.h
===================================================================
--- branches/soc-2008-quorn/source/blender/include/BIF_drawtext.h	2008-06-26 18:15:45 UTC (rev 15363)
+++ branches/soc-2008-quorn/source/blender/include/BIF_drawtext.h	2008-06-26 18:28:33 UTC (rev 15364)
@@ -38,6 +38,7 @@
 
 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);
 

Modified: branches/soc-2008-quorn/source/blender/makesdna/DNA_text_types.h
===================================================================
--- branches/soc-2008-quorn/source/blender/makesdna/DNA_text_types.h	2008-06-26 18:15:45 UTC (rev 15363)
+++ branches/soc-2008-quorn/source/blender/makesdna/DNA_text_types.h	2008-06-26 18:28:33 UTC (rev 15364)
@@ -46,7 +46,7 @@
 	ID id;
 	
 	char *name;
-	
+
 	int flags, nlines;
 	
 	ListBase lines;
@@ -57,6 +57,8 @@
 	int undo_pos, undo_len;
 	
 	void *compiled;
+	
+	time_t  mtime;
 } Text;
 
 

Modified: branches/soc-2008-quorn/source/blender/src/drawtext.c
===================================================================
--- branches/soc-2008-quorn/source/blender/src/drawtext.c	2008-06-26 18:15:45 UTC (rev 15363)
+++ branches/soc-2008-quorn/source/blender/src/drawtext.c	2008-06-26 18:28:33 UTC (rev 15364)
@@ -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 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 @@
 	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 @@
 {
 	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 @@
 			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 @@
 	}
 	
 	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 @@
 		}
 	}
 
+	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();





More information about the Bf-blender-cvs mailing list