# -*- coding: utf-8 -*-
#  Copyright (C) 2012 Christopher Brochtrup
#
#  This file is part of Copy2Clipboard
#
#  Copy2Clipboard 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, either version 3 of the License, or
#  (at your option) any later version.
#
#  Copy2Clipboard is distributed in the hope that it will be useful,
#  but WITHOUT 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 Copy2Clipboard.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
# Description:
#
# Copy2Clipboard will automatically copy a single card field to the clipboard
# when either the card's question side is shown or when the card's answer side
# is shown (or both, if desired).
#
# Default behavior:
#
#   * When the card's question is shown:
#       Nothing will be copied to the clipboard.
#
#   * When the card's answer is shown:
#       The card's "Expression" field will be copied to the clipboard (if one exists).
#
# To change the default behavior simply edit the questionField or answerField
# variables in the User Options section of this file.
#
###############################################################################
# Version: 1.1
# Tested with Anki 2.0.3
# Contact: cb4960@gmail.com
###############################################################################

#### Includes ####

import os, codecs
from anki.hooks import wrap
from PyQt4 import QtGui
from aqt.reviewer import Reviewer

#### User Options ####

# The name of the field to copy to the clipboard when the question side of the
# card is shown. Case sensitive. If you don't want to copy, set to a blank string.
questionField = "Expression"

# The name of the field to copy to the clipboard when the answer side of the
# card is shown. Case sensitive. If you don't want to copy, set to a blank string.
answerField = "Expression"


#### Debugging ####

DEBUG = False
LOG_FILE = "C:/Temp/Copy2Clipboard.log"
CLEAR_LOG_AT_STARTUP = True


#### Functions ####

def clearLog():
    if DEBUG:
        file = codecs.open(LOG_FILE, "w", "utf-8-sig")
        file.write("")
        file.close()


def writeLog(text):
    if DEBUG:
        file = codecs.open(LOG_FILE, "a", "utf-8-sig")
        file.write(text + "\n")
        file.close()


def copyTextToClipboard(text):
    clipboard = QtGui.QApplication.clipboard()
    clipboard.setText(text)


def copyField(fieldToCopy, card):
    for field in card.model()['flds']:
        fieldName = field['name']
        if(fieldName == fieldToCopy):
            value = card.note()[fieldName]
            writeLog(fieldName + ": " + value)
            copyTextToClipboard(value)


def wrapped_showQuestion(self):
    copyField(questionField, self.card)


def wrapped_showAnswer(self):
    copyField(answerField, self.card)


#### Main ####

if CLEAR_LOG_AT_STARTUP:
    clearLog()
writeLog("-----------------------------------------------------------")

writeLog("Main.START")

Reviewer._showQuestion = wrap(Reviewer._showQuestion, wrapped_showQuestion)
Reviewer._showAnswer = wrap(Reviewer._showAnswer, wrapped_showAnswer)
