From 71eea37448283f89e3619325dd1bc30b913d177e Mon Sep 17 00:00:00 2001
From: angelblue05 <angelblue.dev@gmail.com>
Date: Sat, 8 Sep 2018 03:11:44 -0500
Subject: [PATCH] Add libraries option

Display audiobooks under music
---
 .../resource.language.en_gb/strings.po        |  4 ++++
 resources/lib/entrypoint/default.py           |  5 +++-
 resources/lib/entrypoint/service.py           |  6 ++---
 resources/lib/library.py                      | 23 +++++++++++++++----
 4 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po
index 28e2f755..ec2f47ae 100644
--- a/resources/language/resource.language.en_gb/strings.po
+++ b/resources/language/resource.language.en_gb/strings.po
@@ -745,3 +745,7 @@ msgstr ""
 msgctxt "#33153"
 msgid "Your Emby theme media has been synced to Kodi"
 msgstr ""
+
+msgctxt "#33154"
+msgid "Add libraries"
+msgstr ""
diff --git a/resources/lib/entrypoint/default.py b/resources/lib/entrypoint/default.py
index 000ad147..f1b953f7 100644
--- a/resources/lib/entrypoint/default.py
+++ b/resources/lib/entrypoint/default.py
@@ -93,6 +93,8 @@ class Events(object):
             event('RepairLibrarySelection')
         elif mode == 'updatelibs':
             event('SyncLibrarySelection')
+        elif mode == 'addlibs':
+            event('AddLibrarySelection')
         elif mode == 'connect':
             event('EmbyConnect')
         elif mode == 'addserver':
@@ -152,7 +154,7 @@ def listing():
                 directory(label, path, artwork=artwork)
             elif xbmc.getCondVisibility('Window.IsActive(Videos)') and node not in ('photos', 'homevideos', 'music'):
                 directory(label, path, artwork=artwork, context=context)
-            elif xbmc.getCondVisibility('Window.IsActive(Music)') and node == 'music':
+            elif xbmc.getCondVisibility('Window.IsActive(Music)') and node in ('music', 'books', 'audiobooks'):
                 directory(label, path, artwork=artwork, context=context)
             elif not xbmc.getCondVisibility('Window.IsActive(Videos) | Window.IsActive(Pictures) | Window.IsActive(Music)'):
                 directory(label, path, artwork=artwork)
@@ -173,6 +175,7 @@ def listing():
     directory(_(5), "plugin://plugin.video.emby/?mode=settings", False)
     directory(_(33054), "plugin://plugin.video.emby/?mode=adduser", False)
     directory(_(33098), "plugin://plugin.video.emby/?mode=refreshboxsets", False)
+    directory(_(33154), "plugin://plugin.video.emby/?mode=addlibs", False)
     directory(_(33139), "plugin://plugin.video.emby/?mode=updatelibs", False)
     directory(_(33140), "plugin://plugin.video.emby/?mode=repairlibs", False)
     directory(_(33060), "plugin://plugin.video.emby/?mode=thememedia", False)
diff --git a/resources/lib/entrypoint/service.py b/resources/lib/entrypoint/service.py
index 122c0739..95e5d242 100644
--- a/resources/lib/entrypoint/service.py
+++ b/resources/lib/entrypoint/service.py
@@ -171,7 +171,7 @@ class Service(xbmc.Monitor):
                               'LibraryChanged', 'ServerOnline', 'SyncLibrary', 'RepairLibrary', 'RemoveLibrary',
                               'EmbyConnect', 'SyncLibrarySelection', 'RepairLibrarySelection', 'AddServer',
                               'Unauthorized', 'UpdateServer', 'UserConfigurationUpdated', 'ServerRestarting',
-                              'RemoveServer'):
+                              'RemoveServer', 'AddLibrarySelection'):
                 return
 
             data = json.loads(data)[0]
@@ -265,8 +265,8 @@ class Service(xbmc.Monitor):
             window('emby_should_stop.bool', True)
             self.running = False
 
-        elif method in ('SyncLibrarySelection', 'RepairLibrarySelection'):
-            self.library_thread.select_libraries('SyncLibrary' if method == 'SyncLibrarySelection' else 'RepairLibrary')
+        elif method in ('SyncLibrarySelection', 'RepairLibrarySelection', 'AddLibrarySelection'):
+            self.library_thread.select_libraries(method)
 
         elif method == 'SyncLibrary':
             libraries = data['Id'].split(',')
diff --git a/resources/lib/library.py b/resources/lib/library.py
index a777bcd2..c9860eb3 100644
--- a/resources/lib/library.py
+++ b/resources/lib/library.py
@@ -298,17 +298,30 @@ class Library(threading.Thread):
         ''' Select from libraries synced. Either update or repair libraries.
             Send event back to service.py
         '''
-        mode = mode or 'SyncLibrary'
+        modes = {
+            'SyncLibrarySelection': 'SyncLibrary',
+            'RepairLibrarySelection': 'RepairLibrary',
+            'AddLibrarySelection': 'SyncLibrary'
+        }
         sync = get_sync()
         libraries = []
 
         with Database('emby') as embydb:
             db = emby_db.EmbyDatabase(embydb.cursor)
 
-            for library in sync['Whitelist']:
+            if mode in ('SyncLibrarySelection', 'RepairLibrarySelection'):
+                for library in sync['Whitelist']:
 
-                name = db.get_view_name(library.replace('Mixed:', ""))
-                libraries.append({'Id': library, 'Name': name})
+                    name = db.get_view_name(library.replace('Mixed:', ""))
+                    libraries.append({'Id': library, 'Name': name})
+            else:
+                available = [x for x in sync['SortedViews'] if x not in [y.replace('Mixed:', "") for y in sync['Whitelist']]]
+
+                for library in available:
+                    name, media  = db.get_view(library)
+
+                    if media in ('movies', 'tvshows', 'musicvideos', 'mixed', 'music'):
+                        libraries.append({'Id': library, 'Name': name})
 
         choices = [x['Name'] for x in libraries]
         choices.insert(0, _(33121))
@@ -327,7 +340,7 @@ class Library(threading.Thread):
             library = libraries[x - 1]
             selected_libraries.append(library['Id'])
 
-        event(mode, {'Id': ','.join([libraries[x - 1]['Id'] for x in selection])})
+        event(modes[mode], {'Id': ','.join([libraries[x - 1]['Id'] for x in selection])})
 
     def add_library(self, library_id):