minimal working library
[melted_gui] / src / library.c
index c0bcb24..bbda4b8 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * playlist.c -- GTK+ 2 omnplay
- * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
+ * library.c -- GTK+ 2 melted gui
+ * Copyright (C) 2012 Maksym Veremeyenko <verem@m1stereo.tv>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <pthread.h>
 #include <string.h>
 
-#include "omnplay.h"
+#include <mvcp/mvcp.h>
+#include <mvcp/mvcp_remote.h>
+
+#include "instance.h"
 #include "ui.h"
 #include "timecode.h"
 
+void library_release(instance_t* app)
+{
+    mvcp_close(app->library.handle[0]);
+    mvcp_parser_close(app->library.handle[1]);
+};
+
+
+static int library_load_node(instance_t* app, GtkTreeStore *tree_store, GtkTreeIter* parent, char* path)
+{
+    int i;
+    GtkTreeIter iter;
+    mvcp_dir_entry_t entry;
+    mvcp_dir dir = mvcp_dir_init(app->library.handle[0], path);
+
+    for (i = 0; i < mvcp_dir_count(dir); i++)
+    {
+        if(mvcp_ok != mvcp_dir_get(dir, i, &entry))
+            continue;
+
+        g_warning("library_load_node: path=[%s], entry.dur=[%d], entry.full=[%s], entry.name[%s]",
+            path, entry.dir, entry.full, entry.name);
+
+        if(entry.dir)
+        {
+            gtk_tree_store_prepend(tree_store, &iter, parent);
+
+            gtk_tree_store_set(tree_store, &iter,
+                0, app->library.icons[0],
+                1, "<dir>",
+                2, entry.name,
+                3, NULL,
+                4, NULL,
+                5, FALSE,
+                6, "red",
+                -1 );
+
+            library_load_node(app, tree_store, &iter, entry.full);
+        }
+        else
+        {
+            gtk_tree_store_append(tree_store, &iter, parent);
+
+            gtk_tree_store_set(tree_store, &iter,
+                0, app->library.icons[1],
+                1, "<file>",
+                2, entry.name,
+                3, NULL,
+                4, NULL,
+                5, FALSE,
+                6, "red",
+                -1 );
+        };
+    };
+
+    return 0;
+};
+
+static int library_init_load(instance_t* app)
+{
+    int i;
+    char tc[12];
+    GtkTreeStore *tree_store;
+    GtkTreeIter iter;
+
+    tree_store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_tree)));
+    gtk_tree_store_clear(tree_store);
+
+/*
+    gtk_tree_store_append(tree_store, &iter, NULL);
+
+    gtk_tree_store_set(tree_store, &iter,
+        0, NULL,
+        1, "/",
+        2, "<dir>",
+        3, NULL,
+        4, NULL,
+        5, FALSE,
+        6, "red",
+        -1 );
+*/
+
+    library_load_node(app, tree_store, NULL, "/home/studio/Videos");
+
+    gtk_tree_view_collapse_all(GTK_TREE_VIEW(app->library_tree));
+
+    return 0;
+};
+
+
+void library_init(instance_t* app)
+{
+    /* connect to library */
+    app->library.handle[1] = mvcp_parser_init_remote(app->players.host, 5250);
+    app->library.handle[0] = mvcp_init(app->library.handle[1]);
+    if(mvcp_connect(app->library.handle[0]) != mvcp_ok)
+    {
+        g_warning("library_init: failed to connect to server %s", app->players.host);
+        return;
+    };
+
+    app->library.icons[0] = create_pixbuf("Axialis_Team_playlist_open_16x16.png");
+    app->library.icons[1] = create_pixbuf("Axialis_Team_playlist_save_16x16.png");
+
+    library_init_load(app);
+
+#if 0
+    pthread_mutex_lock(&app->library.lock);
+
+    if(app->library.filename[0])
+    {
+        app->library.count = MAX_LIBRARY_ITEMS;
+        omnplay_library_load_file(app->library.item, &app->library.count, app->library.filename);
+    };
+
+    omnplay_library_sort(app);
+
+    pthread_mutex_unlock(&app->library.lock);
+
+    omnplay_library_draw(app);
+#endif
+};
+
+#if 0
 playlist_item_t* omnplay_library_find(omnplay_instance_t* app, char* id)
 {
     int i;
@@ -57,28 +183,68 @@ int omnplay_library_normalize_item(omnplay_instance_t* app, playlist_item_t* ite
 {
     int r = 0;
     playlist_item_t* lib;
+    playlist_item_t prev;
 
     pthread_mutex_lock(&app->library.lock);
 
+    prev = *item;
+
     lib = omnplay_library_find(app, item->id);
 
     item->error = 0;
 
     if(lib)
     {
-
         if(!item->title[0])
         {
             strcpy(item->title, lib->title);
-            r = 1;
+            r++;
         };
 
-        if(!item->dur || item->in < lib->in || (item->in + item->dur) > (lib->in + lib->dur))
+        if(item->in < lib->in || item->in >= (lib->in + lib->dur))
         {
-            item->dur = lib->dur;
             item->in = lib->in;
-            r = 1;
+            r++;
         };
+
+        if(!item->dur || (item->in + item->dur) > (lib->in + lib->dur))
+        {
+            item->dur = lib->in + lib->dur - item->in;
+            r++;
+        };
+
+        if(r)
+            g_warning("omnplay_library_normalize_item: [%s,%d,%d]->[%s,%d,%d]\n",
+                prev.title, prev.in, prev.dur, item->title, item->in, item->dur);
+    }
+    else
+    {
+        r = 1;
+        item->error = PLAYLIST_ITEM_ERROR_LIB;
+    };
+
+    pthread_mutex_unlock(&app->library.lock);
+
+    return r;
+};
+
+int omnplay_library_relink_item(omnplay_instance_t* app, playlist_item_t* item)
+{
+    int r = 0;
+    playlist_item_t* lib;
+
+    pthread_mutex_lock(&app->library.lock);
+
+    lib = omnplay_library_find(app, item->id);
+
+    item->error = 0;
+
+    if(lib)
+    {
+        r = 1;
+        strcpy(item->title, lib->title);
+        item->dur = lib->dur;
+        item->in = lib->in;
     }
     else
     {
@@ -178,22 +344,6 @@ int omnplay_library_load_file(playlist_item_t* items, int *pcount, char* filenam
     return r;
 };
 
-void omnplay_library_load(omnplay_instance_t* app)
-{
-    pthread_mutex_lock(&app->library.lock);
-
-    if(app->library.filename[0])
-    {
-        app->library.count = MAX_LIBRARY_ITEMS;
-        omnplay_library_load_file(app->library.item, &app->library.count, app->library.filename);
-    };
-
-    omnplay_library_sort(app);
-
-    pthread_mutex_unlock(&app->library.lock);
-
-    omnplay_library_draw(app);
-};
 
 static void omnplay_library_save_file(playlist_item_t* item, int count, char* filename)
 {
@@ -443,3 +593,5 @@ void omnplay_library_search(omnplay_instance_t* app, int next)
 
     pthread_mutex_unlock(&app->library.lock);
 };
+
+#endif