|  02-18-2011, 05:34 PM | #1 | 
| Calibre Plugins Developer            Posts: 4,735 Karma: 2197770 Join Date: Oct 2010 Location: Australia Device: Kindle Oasis | 
				
				Can I hold a reference to .model() and .db in a plugin?
			 
			
			To access the data model and database/caches from a plugin, it appears you use self.gui.library_view.model() and self.gui.library_view.model().db respectively. For coding purposes I usually want to hold a shortcut reference to that such as: self.db = self.gui.library_view.model().db to make the code more readable when doing a bunch of db calls. Would it be valid to set this reference just once for your InterfaceAction class in initialization_complete()? Currently I re-set it in each connected event in response to a user action. The reason I do this currently is sheer ignorance from not knowing if other code in Calibre at some point would change the object reference returned from either .model() or .model().db. | 
|   |   | 
|  02-18-2011, 05:47 PM | #2 | |
| Grand Sorcerer            Posts: 12,525 Karma: 8065948 Join Date: Jan 2010 Location: Notts, England Device: Kobo Libra 2 | Quote: 
 | |
|   |   | 
| Advert | |
|  | 
|  02-18-2011, 05:51 PM | #3 | |
| Calibre Plugins Developer            Posts: 4,735 Karma: 2197770 Join Date: Oct 2010 Location: Australia Device: Kindle Oasis | Quote: 
 However that does raise another issue I hadn't considered with my id caches I mentioned in the notification thread. How do I know that the library has been switched in my plugin so I need to invalidate my caches? | |
|   |   | 
|  02-18-2011, 05:55 PM | #4 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			Implement the library_moved method in your plugin
		 | 
|   |   | 
|  02-18-2011, 05:55 PM | #5 | 
| Grand Sorcerer            Posts: 12,525 Karma: 8065948 Join Date: Jan 2010 Location: Notts, England Device: Kobo Libra 2 | 
			
			When the library is changed, the 'library_changed' method in actions is called. Assuming that your plugin defines an action, you supply that method.
		 | 
|   |   | 
| Advert | |
|  | 
|  02-18-2011, 05:58 PM | #6 | 
| Grand Sorcerer            Posts: 12,525 Karma: 8065948 Join Date: Jan 2010 Location: Notts, England Device: Kobo Libra 2 | |
|   |   | 
|  02-18-2011, 06:00 PM | #7 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			Done.
		 | 
|   |   | 
|  02-18-2011, 06:05 PM | #8 | 
| Calibre Plugins Developer            Posts: 4,735 Karma: 2197770 Join Date: Oct 2010 Location: Australia Device: Kindle Oasis | 
			
			Durn it, I remember seeing that function months ago in the API and wondering what the use case was for it.    I guess that "could" be used to set a self.db in response to my original question on this post. Thanks guys. | 
|   |   | 
|  02-18-2011, 06:10 PM | #9 | |
| Grand Sorcerer            Posts: 12,525 Karma: 8065948 Join Date: Jan 2010 Location: Notts, England Device: Kobo Libra 2 | Quote: 
 You can cache the value of self.gui.library_view.model(). I can't think of any instance where the library_view is replaced. | |
|   |   | 
|  02-18-2011, 06:21 PM | #10 | |
| Calibre Plugins Developer            Posts: 4,735 Karma: 2197770 Join Date: Oct 2010 Location: Australia Device: Kindle Oasis | Quote: 
   | |
|   |   | 
|  02-18-2011, 06:21 PM | #11 | |
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | Quote: 
 However, I would encourage you to not hold references to the db unnecessarily. Calling self.gui.library_view.model().db is fast enough that as long as you dont have it in an inner loop you wont see any practical performance implications. | |
|   |   | 
|  02-18-2011, 06:52 PM | #12 | |
| Calibre Plugins Developer            Posts: 4,735 Karma: 2197770 Join Date: Oct 2010 Location: Australia Device: Kindle Oasis | Quote: 
   | |
|   |   | 
|  | 
| Thread Tools | Search this Thread | 
| 
 | 
|  Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
| New Plugin Type Idea: Library Plugin | cgranade | Plugins | 3 | 09-15-2010 12:11 PM | 
| Unutterably Silly Hold your breath and hold down 'x'. (Dumb game.) | frabjous | Lounge | 82 | 11-29-2009 04:17 AM | 
| Reference Books... Do you or don't you? | Stitchawl | Lounge | 12 | 10-31-2009 03:31 PM | 
| PDF Reference | joblack | 5 | 06-12-2009 08:39 AM | |
| E-book for Reference | QFT | Which one should I buy? | 8 | 10-17-2008 10:56 PM |