1 /* gbp-command-bar-suggestion.c
2  *
3  * Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "gbp-command-bar-suggestion"
22 
23 #include "config.h"
24 
25 #include "gbp-command-bar-suggestion.h"
26 
27 struct _GbpCommandBarSuggestion
28 {
29   DzlSuggestion  parent_instance;
30   IdeCommand    *command;
31 } GbpCommandBarSuggestionPrivate;
32 
33 enum {
34   PROP_0,
35   PROP_COMMAND,
36   N_PROPS
37 };
38 
G_DEFINE_FINAL_TYPE(GbpCommandBarSuggestion,gbp_command_bar_suggestion,DZL_TYPE_SUGGESTION)39 G_DEFINE_FINAL_TYPE (GbpCommandBarSuggestion, gbp_command_bar_suggestion, DZL_TYPE_SUGGESTION)
40 
41 static GParamSpec *properties [N_PROPS];
42 
43 static void
44 gbp_command_bar_suggestion_set_command (GbpCommandBarSuggestion *self,
45                                         IdeCommand              *command)
46 {
47   g_return_if_fail (GBP_IS_COMMAND_BAR_SUGGESTION (self));
48   g_return_if_fail (IDE_IS_COMMAND (command));
49 
50   if (g_set_object (&self->command, command))
51     {
52       g_autofree gchar *title = ide_command_get_title (command);
53       g_autofree gchar *subtitle = ide_command_get_subtitle (command);
54 
55       dzl_suggestion_set_title (DZL_SUGGESTION (self), title);
56       dzl_suggestion_set_subtitle (DZL_SUGGESTION (self), subtitle);
57     }
58 }
59 
60 static GIcon *
gbp_command_bar_suggestion_get_icon(DzlSuggestion * suggestion)61 gbp_command_bar_suggestion_get_icon (DzlSuggestion *suggestion)
62 {
63   GbpCommandBarSuggestion *self = (GbpCommandBarSuggestion *)suggestion;
64   IdeCommand *command;
65 
66   g_assert (GBP_IS_COMMAND_BAR_SUGGESTION (self));
67 
68   if ((command = gbp_command_bar_suggestion_get_command (self)))
69     return ide_command_get_icon (command);
70 
71   return NULL;
72 }
73 
74 static void
gbp_command_bar_suggestion_dispose(GObject * object)75 gbp_command_bar_suggestion_dispose (GObject *object)
76 {
77   GbpCommandBarSuggestion *self = (GbpCommandBarSuggestion *)object;
78 
79   g_clear_object (&self->command);
80 
81   G_OBJECT_CLASS (gbp_command_bar_suggestion_parent_class)->dispose (object);
82 }
83 
84 static void
gbp_command_bar_suggestion_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)85 gbp_command_bar_suggestion_get_property (GObject    *object,
86                                          guint       prop_id,
87                                          GValue     *value,
88                                          GParamSpec *pspec)
89 {
90   GbpCommandBarSuggestion *self = GBP_COMMAND_BAR_SUGGESTION (object);
91 
92   switch (prop_id)
93     {
94     case PROP_COMMAND:
95       g_value_set_object (value, gbp_command_bar_suggestion_get_command (self));
96       break;
97 
98     default:
99       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100     }
101 }
102 
103 static void
gbp_command_bar_suggestion_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)104 gbp_command_bar_suggestion_set_property (GObject      *object,
105                                          guint         prop_id,
106                                          const GValue *value,
107                                          GParamSpec   *pspec)
108 {
109   GbpCommandBarSuggestion *self = GBP_COMMAND_BAR_SUGGESTION (object);
110 
111   switch (prop_id)
112     {
113     case PROP_COMMAND:
114       gbp_command_bar_suggestion_set_command (self, g_value_get_object (value));
115       break;
116 
117     default:
118       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119     }
120 }
121 
122 static void
gbp_command_bar_suggestion_class_init(GbpCommandBarSuggestionClass * klass)123 gbp_command_bar_suggestion_class_init (GbpCommandBarSuggestionClass *klass)
124 {
125   GObjectClass *object_class = G_OBJECT_CLASS (klass);
126   DzlSuggestionClass *suggestion_class = DZL_SUGGESTION_CLASS (klass);
127 
128   object_class->dispose = gbp_command_bar_suggestion_dispose;
129   object_class->get_property = gbp_command_bar_suggestion_get_property;
130   object_class->set_property = gbp_command_bar_suggestion_set_property;
131 
132   suggestion_class->get_icon = gbp_command_bar_suggestion_get_icon;
133 
134   properties [PROP_COMMAND] =
135     g_param_spec_object ("command",
136                          "Command",
137                          "The command for the suggestion",
138                          IDE_TYPE_COMMAND,
139                          (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
140 
141   g_object_class_install_properties (object_class, N_PROPS, properties);
142 }
143 
144 static void
gbp_command_bar_suggestion_init(GbpCommandBarSuggestion * self)145 gbp_command_bar_suggestion_init (GbpCommandBarSuggestion *self)
146 {
147 }
148 
149 /**
150  * gbp_command_bar_suggestion_get_command:
151  * @self: a #GbpCommandBarSuggestion
152  *
153  * Returns: (transfer none): an #IdeCommand
154  *
155  * Since: 3.32
156  */
157 IdeCommand *
gbp_command_bar_suggestion_get_command(GbpCommandBarSuggestion * self)158 gbp_command_bar_suggestion_get_command (GbpCommandBarSuggestion *self)
159 {
160   g_return_val_if_fail (GBP_IS_COMMAND_BAR_SUGGESTION (self), NULL);
161 
162   return self->command;
163 }
164 
165 GbpCommandBarSuggestion *
gbp_command_bar_suggestion_new(IdeCommand * command)166 gbp_command_bar_suggestion_new (IdeCommand *command)
167 {
168   g_return_val_if_fail (IDE_IS_COMMAND (command), NULL);
169 
170   return g_object_new (GBP_TYPE_COMMAND_BAR_SUGGESTION,
171                        "command", command,
172                        NULL);
173 }
174