# Opera Widgets Preference Store

## Introduction

The Opera Widgets Preference Store is where widgets store their settings and other data you want the widget to persist across sessions. Once data is stored in the Opera Widgets Preference Store, the user may close and reopen the widget, and the stored preferences will still be available.

## What is the Preference Store?

The preference store is a key/value store with both the key and the value being strings. The widget may store data to be reused at a later date, such as a username, settings for the widget, cached data, and so on. The data is stored on the device, for example on its hard drive, and can be read back at any time. This is also the case if the widget is restarted or the device or the widget runner crashes.

The data for a widget is only available to that widget, not to other widgets or Web pages.

## Using the Preference Store

You work with the Preference Store using two methods on the `widget` object: `setPreferenceForKey` and `preferenceForKey`. `setPreferenceForKey` takes two arguments, the value to save and the name of the key. `preferenceForKey` takes the name of the key to retrieve the value of as an argument. For example:

    widget.setPreferenceForKey( 'gautamc', 'username' );
    //Close and reopen the widget
    var username = widget.preferenceForKey('username');

The value is stored immediately, so you do not need to reopen the widget to be able to access it. If you store a value for a key that already exists, the old value will be silently overwritten.

Note: The methods are named after the similar methods that exist for Mac OS X Dashboard widgets, in order to promote compatibility.

## Storage capacity considerations

Some devices have relatively limited storage capacity available for storing widget preference data. If a widget attempts to store data beyond the free space available, the device will free space by deleting preferences for older widgets in order of the time they were last run.

Example: For the sake of argument, assume that the your device has space for just two sets of widget preferences and that newly-installed widgets all store preferences. Say you download the BBC News widget. You then download the 2d ruler the next day, use it, and then use the BBC News widget. If you then download the Earthquake monitor widget on the third day and this widget stores preferences, all the preferences for the 2d ruler would be deleted to make space for the new ones, as it is the widget that was used the longest time ago.

By design, all preferences for a single widget are either fully deleted or left intact. No single preference is deleted to make room for another widget’s preferences. This means that you can be sure that the stored widget preferences are in a consistent state.

Note that while all preferences for a widget will be deleted, this may not be the case for the widget’s cookies and cache.

## Resources

- Opera Widgets Specification 1.0, third edition