#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Canonical
#
# Authors:
#  Marco Trevisan <marco.trevisan@canonical.com>
#
# 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 the Free Software
# Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUTa
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from gi.repository import Gio
import os,sys

GNOME_UI_SETTINGS = "org.gnome.desktop.interface";
GNOME_TEXT_SCALE_FACTOR = "text-scaling-factor";

UNITY_UI_SETTINGS = "com.canonical.Unity.Interface";
UNITY_UI_SETTINGS_PATH = "/com/canonical/unity/interface/"
UNITY_TEXT_SCALE_FACTOR = "text-scale-factor";

if GNOME_UI_SETTINGS not in Gio.Settings.list_schemas():
    print("No gnome desktop interface schemas found, no migration needed")
    sys.exit(0)

text_scale_factor = Gio.Settings(schema=GNOME_UI_SETTINGS).get_double(GNOME_TEXT_SCALE_FACTOR)

# gsettings doesn't work directly, the key is somewhat reverted. Work one level under then: dconf!
# Gio.Settings(schema=UNITY_UI_SETTINGS).set_int(UNITY_TEXT_SCALE_FACTOR, text_scale_factor)
from subprocess import Popen, PIPE, STDOUT
p = Popen(("dconf load "+UNITY_UI_SETTINGS_PATH).split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.communicate(input="[/]\n"+UNITY_TEXT_SCALE_FACTOR+"={}".format(text_scale_factor).encode('utf-8'))
