From 5800461f1dee671c1d3607d4d7018f1293a20f84 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 16 Jun 2013 02:25:04 +0100
Subject: [PATCH] [Add] Possibility to delete save games.

---
 src/save.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/save.c b/src/save.c
index a2e5032..ce9715a 100644
--- a/src/save.c
+++ b/src/save.c
@@ -1,3 +1,7 @@
+#ifdef _POSIX_SOURCE
+#include <unistd.h> // Unlink.
+#endif
+
 #include "lephisto.h"
 #include "log.h"
 #include "xml.h"
@@ -29,6 +33,7 @@ extern void menu_main_close(void);
 static int  save_data(xmlTextWriterPtr writer);
 static void load_menu_close(char* str);
 static void load_menu_load(char* str);
+static void load_menu_delete(char* str);
 static int  load_game(char* file);
 
 // Save all the game data.
@@ -131,6 +136,9 @@ void load_game_menu(void) {
   window_addButton(wid, -20, 30 + BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT,
       "btnLoad", "Load", load_menu_load);
 
+  window_addButton(wid, -20, 20+2*(10+BUTTON_HEIGHT), BUTTON_WIDTH, BUTTON_HEIGHT,
+      "btnDelete", "Del", load_menu_delete);
+
   // Default action.
   window_setFptr(wid, load_menu_load);
 }
@@ -159,6 +167,29 @@ static void load_menu_load(char* str) {
   menu_main_close();
 }
 
+static void load_menu_delete(char* str) {
+  (void)str;
+  char* save, path[PATH_MAX];
+  int wid;
+
+  wid = window_get("Load Game");
+  save = toolkit_getList(wid, "lstSaves");
+
+  if(strcmp(save, "None") == 0)
+    return;
+
+  if(dialogue_YesNo("Permanently Delete?",
+        "Are you sure you want to permanently delete '%s'?", save) == 0)
+    return;
+
+  snprintf(path, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), save);
+  unlink(path);
+
+  // Need to reload the menu.
+  load_menu_close(NULL);
+  load_game_menu();
+}
+
 // Load a new game.
 static int load_game(char* file) {
   xmlNodePtr node;