rectifyer/script.trakt — pull request #108 by nate1280 (2013-05-01 13:27:52+00:00)
Title: Add function to ToggleWatched

## Overview:
Since XBMC's python interface does not offer an event that triggers when an item's playcount changes, I decided to add a wrapper around XBMC's built in action ToggleWatched.

## Supported Methods:
There is only one way to use this:
```
RunScript(script.trakt,action=togglewatched)
```

## Information:
This new action, will ever only work on library items, consider it an extension of the sync features of the trakt script.

Best usage for this, would be to add the following to your keyboard.xml
```

RunScript(script.trakt,action=togglewatched)

```
This will override XBMC's default action for W causing a ToggleWatched in the video library.

This works on all levels of the video library, movie, show, seasons, and episodes.

When an item or items are successfully marked as watched, a notification will popup indicating this, this can be toggled on/off via a setting.

Since this is an extension of the sync features, it also uses the simulate setting, so if this is enabled, it will do everything but actually update trakt.tv. It will still do XBMC's built in ToggleWatched tho.

**Note:** this addition only works on library items, it won't be updated to support non-library items due to the amount of data that could potentially need to be passed along, and also, since it essentially wraps around XBMC's built in ToggleWatched action.

I've also added a fix for the BadStatusLine exception that started occuring a few days ago, I might split this out and open a PR of its own for it, have not decided yet.

## How it works
- For movies and episodes, it gets the item's playcount and other relevant data.

- For seasons, it will get the playcounts of all the items in the selected season, and add episodes to a list if there playcount is 0.

- For shows, you guessed it, it will get data for all the episodes, and add those episodes whose playcount is 0 to a list.

- If nothing is going has a playcount of 0, it skips dispatching to the service thread. Otherwise, it dispatches a new action to the scripts service thread with the data gathered above.

- It will now execute ToggleWatched on the item, so XBMC does its normal thing here.

- The service thread will take the data it got, and lookup information on trakt and filter out any items that are already watched, and finally, mark those that aren't as watched.

## Technical
This pull request introduces a new system for the dispatch queue. Before it used python's built in Queue class to handle this.

Since branching out to actions in the script, it can not communicate easily with the service thread. I was using window properties to trigger specific events, but, with manual rating, and this new addition, that could introduce race conditions.

So, I redid the dispatch system by using a sqlite database as the queue. This eliminates any potential race conditions, lets script functions dispatch queue events that the service thread will pick up on, and is also thread safe.

I've gone through my movie list rapidly toggling movies as watched, and they all went through without a hiccup, the previous method, had issues with this.

Since I would consider this a core change, it needs to be thoroughly tested, on any platforms. I run this on my HTPC, and everything works as expected, but, still needs testing.

## Other
As usual, enjoy, and feedback/etc is greatly welcome.

## Changes:
- Add a new action, togglewatched to wrap around the XBMC action, works only on library items, consider it an extension of the sync functions.
- Add settings to toggle notifications on/off for items marked as watched.
- ToggleWatched action uses the simulate_sync setting, so it can be easily tested.
- ToggleWatched will only mark as watched on trakt, it will not unwatch items, it checks XBMC data to build a list of what has a playcount of 0, and also checks data from trakt to see if the item is not watched already.
- Add new queue system for the dispatch queue, uses an sqlite database, this allows for script functions to send events to the service thread without the need to set properties which could cause race conditions.
- Update manual sync and manual rating to use new queue system.
- Update manual sync to get XBMC data in script call, instead of service, and pass this data, less to do in service thread now, makes it cleaner.
Add new trakt api call, show/season to get season data for marking a season as watched
- Add extend option to getShowSummary
Fix for BadStatusLine error.