Add _xed_document_needs_saving

Based on acbf4d4f0c
This commit is contained in:
JosephMcc 2017-01-29 03:28:52 -08:00
parent 511fc3dfec
commit 5071bfe065
4 changed files with 140 additions and 147 deletions

View File

@ -806,24 +806,6 @@ _xed_cmd_file_save_as (GtkAction *action,
file_save_as (tab, window); file_save_as (tab, window);
} }
static gboolean
document_needs_saving (XedDocument *doc)
{
if (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)))
{
return TRUE;
}
/* we check if it was deleted only for local files
* since for remote files it may hang */
if (xed_document_is_local (doc) && xed_document_get_deleted (doc))
{
return TRUE;
}
return FALSE;
}
/* /*
* The docs in the list must belong to the same XedWindow. * The docs in the list must belong to the same XedWindow.
*/ */
@ -863,7 +845,7 @@ _xed_cmd_file_save_documents_list (XedWindow *window,
user is running xed - Paolo (Dec. 8, 2005) */ user is running xed - Paolo (Dec. 8, 2005) */
if (xed_document_is_untitled (doc) || xed_document_get_readonly (doc)) if (xed_document_is_untitled (doc) || xed_document_get_readonly (doc))
{ {
if (document_needs_saving (doc)) if (_xed_document_needs_saving (doc))
{ {
tabs_to_save_as = g_slist_prepend (tabs_to_save_as, t); tabs_to_save_as = g_slist_prepend (tabs_to_save_as, t);
} }
@ -1214,7 +1196,7 @@ tab_state_changed_while_saving (XedTab *tab,
/* If the saving operation failed or was interrupted, then the /* If the saving operation failed or was interrupted, then the
document is still "modified" -> do not close the tab */ document is still "modified" -> do not close the tab */
if (document_needs_saving (doc)) if (_xed_document_needs_saving (doc))
{ {
return; return;
} }
@ -1327,7 +1309,7 @@ save_and_close_all_documents (const GList *docs,
(state != XED_TAB_STATE_REVERTING)) /* CHECK: is this the right behavior with REVERTING ?*/ (state != XED_TAB_STATE_REVERTING)) /* CHECK: is this the right behavior with REVERTING ?*/
{ {
/* The document must be saved before closing */ /* The document must be saved before closing */
g_return_if_fail (document_needs_saving (doc)); g_return_if_fail (_xed_document_needs_saving (doc));
/* FIXME: manage the case of local readonly files owned by the /* FIXME: manage the case of local readonly files owned by the
user is running xed - Paolo (Dec. 8, 2005) */ user is running xed - Paolo (Dec. 8, 2005) */

View File

@ -1674,6 +1674,37 @@ xed_document_get_deleted (XedDocument *doc)
return doc->priv->deleted; return doc->priv->deleted;
} }
/*
* Deletion and external modification is only checked for local files.
*/
gboolean
_xed_document_needs_saving (XedDocument *doc)
{
g_return_val_if_fail (XED_IS_DOCUMENT (doc), FALSE);
if (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)))
{
return TRUE;
}
if (doc->priv->externally_modified || doc->priv->deleted)
{
return TRUE;
}
if (xed_document_is_local (doc))
{
check_file_on_disk (doc);
if (doc->priv->externally_modified || doc->priv->deleted)
{
return TRUE;
}
}
return FALSE;
}
/* /*
* If @line is bigger than the lines of the document, the cursor is moved * If @line is bigger than the lines of the document, the cursor is moved
* to the last line and FALSE is returned. * to the last line and FALSE is returned.

View File

@ -41,9 +41,6 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/*
* Type checking and casting macros
*/
#define XED_TYPE_DOCUMENT (xed_document_get_type()) #define XED_TYPE_DOCUMENT (xed_document_get_type())
#define XED_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), XED_TYPE_DOCUMENT, XedDocument)) #define XED_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), XED_TYPE_DOCUMENT, XedDocument))
#define XED_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), XED_TYPE_DOCUMENT, XedDocumentClass)) #define XED_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), XED_TYPE_DOCUMENT, XedDocumentClass))
@ -57,9 +54,9 @@ G_BEGIN_DECLS
typedef enum typedef enum
{ {
XED_DOCUMENT_NEWLINE_TYPE_LF, XED_DOCUMENT_NEWLINE_TYPE_LF,
XED_DOCUMENT_NEWLINE_TYPE_CR, XED_DOCUMENT_NEWLINE_TYPE_CR,
XED_DOCUMENT_NEWLINE_TYPE_CR_LF XED_DOCUMENT_NEWLINE_TYPE_CR_LF
} XedDocumentNewlineType; } XedDocumentNewlineType;
#define XED_DOCUMENT_NEWLINE_TYPE_DEFAULT XED_DOCUMENT_NEWLINE_TYPE_LF #define XED_DOCUMENT_NEWLINE_TYPE_DEFAULT XED_DOCUMENT_NEWLINE_TYPE_LF
@ -72,67 +69,58 @@ typedef enum
*/ */
typedef enum typedef enum
{ {
XED_DOCUMENT_SAVE_IGNORE_MTIME = 1 << 0, XED_DOCUMENT_SAVE_IGNORE_MTIME = 1 << 0,
XED_DOCUMENT_SAVE_IGNORE_BACKUP = 1 << 1, XED_DOCUMENT_SAVE_IGNORE_BACKUP = 1 << 1,
XED_DOCUMENT_SAVE_PRESERVE_BACKUP = 1 << 2, XED_DOCUMENT_SAVE_PRESERVE_BACKUP = 1 << 2,
XED_DOCUMENT_SAVE_IGNORE_INVALID_CHARS = 1 << 3 XED_DOCUMENT_SAVE_IGNORE_INVALID_CHARS = 1 << 3
} XedDocumentSaveFlags; } XedDocumentSaveFlags;
/* Private structure type */ typedef struct _XedDocument XedDocument;
typedef struct _XedDocumentPrivate XedDocumentPrivate; typedef struct _XedDocumentPrivate XedDocumentPrivate;
typedef struct _XedDocumentClass XedDocumentClass;
/*
* Main object structure
*/
typedef struct _XedDocument XedDocument;
struct _XedDocument struct _XedDocument
{ {
GtkSourceBuffer buffer; GtkSourceBuffer buffer;
/*< private > */ /*< private > */
XedDocumentPrivate *priv; XedDocumentPrivate *priv;
}; };
/*
* Class definition
*/
typedef struct _XedDocumentClass XedDocumentClass;
struct _XedDocumentClass struct _XedDocumentClass
{ {
GtkSourceBufferClass parent_class; GtkSourceBufferClass parent_class;
/* Signals */ // CHECK: ancora da rivedere /* Signals */ // CHECK: ancora da rivedere
void (* cursor_moved) (XedDocument *document); void (* cursor_moved) (XedDocument *document);
/* Document load */ /* Document load */
void (* load) (XedDocument *document, void (* load) (XedDocument *document,
GFile *location, GFile *location,
const XedEncoding *encoding, const XedEncoding *encoding,
gint line_pos, gint line_pos,
gboolean create); gboolean create);
void (* loading) (XedDocument *document, void (* loading) (XedDocument *document,
goffset size, goffset size,
goffset total_size); goffset total_size);
void (* loaded) (XedDocument *document, void (* loaded) (XedDocument *document,
const GError *error); const GError *error);
/* Document save */ /* Document save */
void (* save) (XedDocument *document, void (* save) (XedDocument *document,
GFile *location, GFile *location,
const XedEncoding *encoding, const XedEncoding *encoding,
XedDocumentSaveFlags flags); XedDocumentSaveFlags flags);
void (* saving) (XedDocument *document, void (* saving) (XedDocument *document,
goffset size, goffset size,
goffset total_size); goffset total_size);
void (* saved) (XedDocument *document, void (* saved) (XedDocument *document,
const GError *error); const GError *error);
}; };
@ -140,104 +128,96 @@ struct _XedDocumentClass
enum enum
{ {
XED_DOCUMENT_ERROR_EXTERNALLY_MODIFIED, XED_DOCUMENT_ERROR_EXTERNALLY_MODIFIED,
XED_DOCUMENT_ERROR_CANT_CREATE_BACKUP, XED_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
XED_DOCUMENT_ERROR_TOO_BIG, XED_DOCUMENT_ERROR_TOO_BIG,
XED_DOCUMENT_ERROR_ENCODING_AUTO_DETECTION_FAILED, XED_DOCUMENT_ERROR_ENCODING_AUTO_DETECTION_FAILED,
XED_DOCUMENT_ERROR_CONVERSION_FALLBACK, XED_DOCUMENT_ERROR_CONVERSION_FALLBACK,
XED_DOCUMENT_NUM_ERRORS XED_DOCUMENT_NUM_ERRORS
}; };
GQuark xed_document_error_quark (void); GQuark xed_document_error_quark (void);
GType xed_document_get_type (void) G_GNUC_CONST; GType xed_document_get_type (void) G_GNUC_CONST;
XedDocument *xed_document_new (void); XedDocument *xed_document_new (void);
GFile *xed_document_get_location (XedDocument *doc); GFile *xed_document_get_location (XedDocument *doc);
void xed_document_set_location (XedDocument *doc, void xed_document_set_location (XedDocument *doc,
GFile *location); GFile *location);
gchar *xed_document_get_uri_for_display gchar *xed_document_get_uri_for_display (XedDocument *doc);
(XedDocument *doc);
gchar *xed_document_get_short_name_for_display
(XedDocument *doc);
void xed_document_set_short_name_for_display gchar *xed_document_get_short_name_for_display (XedDocument *doc);
(XedDocument *doc,
const gchar *name);
gchar *xed_document_get_content_type void xed_document_set_short_name_for_display (XedDocument *doc,
(XedDocument *doc); const gchar *name);
void xed_document_set_content_type gchar *xed_document_get_content_type (XedDocument *doc);
(XedDocument *doc,
const gchar *content_type);
gchar *xed_document_get_mime_type (XedDocument *doc); void xed_document_set_content_type (XedDocument *doc,
const gchar *content_type);
gboolean xed_document_get_readonly (XedDocument *doc); gchar *xed_document_get_mime_type (XedDocument *doc);
void xed_document_load (XedDocument *doc, gboolean xed_document_get_readonly (XedDocument *doc);
GFile *location,
const XedEncoding *encoding,
gint line_pos,
gboolean create);
gboolean xed_document_load_cancel (XedDocument *doc); void xed_document_load (XedDocument *doc,
GFile *location,
const XedEncoding *encoding,
gint line_pos,
gboolean create);
void xed_document_save (XedDocument *doc, gboolean xed_document_load_cancel (XedDocument *doc);
XedDocumentSaveFlags flags);
void xed_document_save_as (XedDocument *doc, void xed_document_save (XedDocument *doc,
GFile *location, XedDocumentSaveFlags flags);
const XedEncoding *encoding,
XedDocumentSaveFlags flags);
gboolean xed_document_is_untouched (XedDocument *doc); void xed_document_save_as (XedDocument *doc,
gboolean xed_document_is_untitled (XedDocument *doc); GFile *location,
const XedEncoding *encoding,
XedDocumentSaveFlags flags);
gboolean xed_document_is_local (XedDocument *doc); gboolean xed_document_is_untouched (XedDocument *doc);
gboolean xed_document_is_untitled (XedDocument *doc);
gboolean xed_document_get_deleted (XedDocument *doc); gboolean xed_document_is_local (XedDocument *doc);
gboolean xed_document_goto_line (XedDocument *doc, gboolean xed_document_get_deleted (XedDocument *doc);
gint line);
gboolean xed_document_goto_line_offset(XedDocument *doc, gboolean xed_document_goto_line (XedDocument *doc,
gint line, gint line);
gint line_offset);
void xed_document_set_language (XedDocument *doc, gboolean xed_document_goto_line_offset (XedDocument *doc,
GtkSourceLanguage *lang); gint line,
GtkSourceLanguage gint line_offset);
*xed_document_get_language (XedDocument *doc);
const XedEncoding void xed_document_set_language (XedDocument *doc,
*xed_document_get_encoding (XedDocument *doc); GtkSourceLanguage *lang);
GtkSourceLanguage *xed_document_get_language (XedDocument *doc);
void xed_document_set_newline_type (XedDocument *doc, const XedEncoding *xed_document_get_encoding (XedDocument *doc);
XedDocumentNewlineType newline_type);
XedDocumentNewlineType void xed_document_set_newline_type (XedDocument *doc,
xed_document_get_newline_type (XedDocument *doc); XedDocumentNewlineType newline_type);
gchar *xed_document_get_metadata (XedDocument *doc, XedDocumentNewlineType xed_document_get_newline_type (XedDocument *doc);
const gchar *key);
void xed_document_set_metadata (XedDocument *doc, gchar *xed_document_get_metadata (XedDocument *doc,
const gchar *first_key, const gchar *key);
...);
void xed_document_set_metadata (XedDocument *doc,
const gchar *first_key,
...);
/* /*
* Non exported functions * Non exported functions
*/ */
void _xed_document_set_readonly (XedDocument *doc, void _xed_document_set_readonly (XedDocument *doc,
gboolean readonly); gboolean readonly);
glong _xed_document_get_seconds_since_last_save_or_load glong _xed_document_get_seconds_since_last_save_or_load (XedDocument *doc);
(XedDocument *doc);
void _xed_document_apply_error_style (XedDocument *doc, void _xed_document_apply_error_style (XedDocument *doc,
GtkTextIter *start, GtkTextIter *start,
@ -248,19 +228,17 @@ void _xed_document_apply_error_style (XedDocument *doc,
GtkTextIter *end); GtkTextIter *end);
/* Note: this is a sync stat: use only on local files */ /* Note: this is a sync stat: use only on local files */
gboolean _xed_document_check_externally_modified gboolean _xed_document_check_externally_modified (XedDocument *doc);
(XedDocument *doc);
gboolean _xed_document_needs_saving (XedDocument *doc);
typedef GMountOperation *(*XedMountOperationFactory)(XedDocument *doc, typedef GMountOperation *(*XedMountOperationFactory)(XedDocument *doc,
gpointer userdata); gpointer userdata);
void _xed_document_set_mount_operation_factory void _xed_document_set_mount_operation_factory (XedDocument *doc,
(XedDocument *doc, XedMountOperationFactory callback,
XedMountOperationFactory callback, gpointer userdata);
gpointer userdata); GMountOperation *_xed_document_create_mount_operation (XedDocument *doc);
GMountOperation
*_xed_document_create_mount_operation
(XedDocument *doc);
void _xed_document_set_search_context (XedDocument *doc, void _xed_document_set_search_context (XedDocument *doc,
GtkSourceSearchContext *search_context); GtkSourceSearchContext *search_context);

View File

@ -2501,10 +2501,12 @@ _xed_tab_can_close (XedTab *tab)
doc = xed_tab_get_document (tab); doc = xed_tab_get_document (tab);
/* TODO: we need to save the file also if it has been externally if (_xed_document_needs_saving (doc))
modified - Paolo (Oct 10, 2005) */ {
return FALSE;
}
return (!gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)) && !xed_document_get_deleted (doc)); return TRUE;
} }
/** /**