1/*
2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Library General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Library General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Library General Public License
15 *  along with this library; see the file COPYING.LIB.  If not, write to
16 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 *  Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "MediaQueryList.h"
22
23#include "MediaList.h"
24#include "MediaQueryEvaluator.h"
25#include "MediaQueryListListener.h"
26#include "MediaQueryMatcher.h"
27
28namespace WebCore {
29
30PassRefPtr<MediaQueryList> MediaQueryList::create(PassRefPtr<MediaQueryMatcher> vector, PassRefPtr<MediaQuerySet> media, bool matches)
31{
32    return adoptRef(new MediaQueryList(vector, media, matches));
33}
34
35MediaQueryList::MediaQueryList(PassRefPtr<MediaQueryMatcher> vector, PassRefPtr<MediaQuerySet> media, bool matches)
36    : m_matcher(vector)
37    , m_media(media)
38    , m_evaluationRound(m_matcher->evaluationRound())
39    , m_changeRound(m_evaluationRound - 1) // m_evaluationRound and m_changeRound initial values must be different.
40    , m_matches(matches)
41{
42}
43
44MediaQueryList::~MediaQueryList()
45{
46}
47
48String MediaQueryList::media() const
49{
50    return m_media->mediaText();
51}
52
53void MediaQueryList::addListener(PassRefPtr<MediaQueryListListener> listener)
54{
55    if (!listener)
56        return;
57
58    m_matcher->addListener(listener, this);
59}
60
61void MediaQueryList::removeListener(PassRefPtr<MediaQueryListListener> listener)
62{
63    if (!listener)
64        return;
65
66    m_matcher->removeListener(listener.get(), this);
67}
68
69void MediaQueryList::evaluate(MediaQueryEvaluator* evaluator, bool& notificationNeeded)
70{
71    if (m_evaluationRound != m_matcher->evaluationRound() && evaluator)
72        setMatches(evaluator->eval(m_media.get()));
73    notificationNeeded = m_changeRound == m_matcher->evaluationRound();
74}
75
76void MediaQueryList::setMatches(bool newValue)
77{
78    m_evaluationRound = m_matcher->evaluationRound();
79
80    if (newValue == m_matches)
81        return;
82
83    m_matches = newValue;
84    m_changeRound = m_evaluationRound;
85}
86
87bool MediaQueryList::matches()
88{
89    if (m_evaluationRound != m_matcher->evaluationRound())
90        setMatches(m_matcher->evaluate(m_media.get()));
91    return m_matches;
92}
93
94}
95