blib/lib/Desktop/KDEActivity/Util.pm

filter: Statementstmt 28.7% 29 / 101 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 46.1% 6 / 13 Podpod 100% 3 / 3 totaltotal 23.4% 38 / 162 CC 29 SCAR 51.0
CC
29
Cov
20%
_list_kde_activities
342.0
set_current_kde_activity
56.0
get_current_kde_activity
30.0
CRAP 164.0
↑ Summary
1 package Desktop::KDEActivity::Util;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IPC::System::Options 'system', -log=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2026-05-18'; # DATE
13 1 our $DIST = 'Desktop-KDEActivity-Util'; # DIST
14 1 our $VERSION = '0.003'; # VERSION
15  
16 1 our @EXPORT_OK = qw(
17 get_current_kde_activity
18 set_current_kde_activity
19 list_kde_activities
20 );
21  
22 1 our %SPEC;
23  
24 1 $SPEC{':package'} = {
25 v => 1.1,
26 summary => 'Utilities related to KDE Activities',
27 };
28  
29 0 ▶sub _list_kde_activities {
Subroutine _list_kde_activities: not called CC=18 SCAR=58.3
30 0 my $which = shift;
31 0 my %args = @_;
32  
33 0 ▶ system({capture_stdout => \my $stdout},
Branch: 1 branch
BranchTrueFalseTotal
$which eq 'list' ? : 0 0 0
34 "kactivities-cli", ($which eq 'list' ? ("--list-activities") : ('--current-activity')));
35 0 ▶ return [500, "Can't run kactivities-cli"] if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
36 0 my @rows;
37  
38 0 for my $line (split /^/m, $stdout) {
39 0 my ($status, $guid, $name, $icon) = $line =~ /^\[(.+?)\] ([0-9a-f-]+) (.+?) \((.*?)\)/;
40 0 ▶ push @rows, {
Branch: 2 branches
BranchTrueFalseTotal
$status =~ /RUNNING|CURRENT/ ? : 0 0 0
$status =~ /CURRENT/ ? : 0 0 0
41 is_running => ($status =~ /RUNNING|CURRENT/ ? 1:0),
42 is_current => ($status =~ /CURRENT/ ? 1:0),
43 guid => $guid,
44 name => $name,
45 icon => $icon,
46 };
47 }
48  
49 FILTER_HAS_WINDOWS: {
50 0 ▶ last unless defined $args{has_windows};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"has_windows"} 0 0 0
51 0 require Desktop::XWindowManager::Util;
52 0 my $res = Desktop::XWindowManager::Util::list_xwm_windows();
53 0 ▶ return [500, "Can't list windows: $res->[0] - $res->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
54 unless $res->[0] == 200;
55 0 my %act_num_windows;
56 0 for my $row (@rows) { $act_num_windows{ $row->{guid} } = 0 }
57 0 for my $wid (@{$res->[2]}) {
58 0 my $res_gwkact = Desktop::XWindowManager::Util::get_xwm_window_kde_activity(
59 id => $wid);
60 0 ▶ unless ($res_gwkact->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res_gwkact->[0] == 200) 0 0 0
61 0 log_warn "Can't get KDE activity for window id %s: %d - %s", $wid, $res_gwkact->[0], $res_gwkact->[1];
62 0 last FILTER_HAS_WINDOWS;
63 }
64 0 my $guids = $res_gwkact->[2];
65 0 ▶ my @guids = $guids ? (split /,/, $guids) : ();
Branch: 1 branch
BranchTrueFalseTotal
$guids ? : 0 0 0
66 0 for my $guid (@guids) { $act_num_windows{ $guid }++ }
67 }
68  
69 0 my @filtered_rows;
70 0 for my $row (@rows) {
71 0 $row->{num_windows} = $act_num_windows{ $row->{guid} };
72 0 ▶ if ($args{has_windows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'has_windows'}) { } 0 0 0
73 0 ▶ push @filtered_rows, $row if $row->{num_windows};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"num_windows"} 0 0 0
74 } else {
75 0 ▶ push @filtered_rows, $row unless $row->{num_windows};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"num_windows"} 0 0 0
76 }
77 }
78 0 @rows = @filtered_rows;
79 } # FILTER_HAS_WINDOWS
80  
81 0 ▶ unless ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"detail"}) 0 0 0
82 0 @rows = map { $_->{name} } @rows;
83 }
84  
85 0 return [200, "OK", \@rows];
86 }
87  
88 $SPEC{list_kde_activities} = {
89 v => 1.1,
90 summary => "List all known KDE activities",
91 args => {
92 detail => {
93 schema => 'bool*',
94 cmdline_aliases => {l=>{}},
95 },
96 has_windows => {
97 schema => 'bool*',
98 cmdline_aliases => {
99 0 ▶ w=>{is_flag=>1, summary=>'Shortcut for --has-windows', code=>sub { $_[0]{has_windows} = 1 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 ▶ W=>{is_flag=>1, summary=>'Shortcut for --no-has-windows (--hasnt-windows)', code=>sub { $_[0]{has_windows} = 0 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 },
102 1 tags => ['category:filtering'],
103 },
104 },
105 deps => {
106 prog => 'kactivities-cli',
107 },
108 };
109 0 ▶sub list_kde_activities {
Subroutine list_kde_activities: not called CC=1 SCAR=6.9
Pod documented
110 0 _list_kde_activities('list', @_);
111 }
112  
113 $SPEC{get_current_kde_activity} = {
114 1 v => 1.1,
115 summary => "Return the name of the current KDE activity",
116 args => {
117 detail => {
118 schema => 'bool*',
119 cmdline_aliases => {l=>{}},
120 },
121 guid => {
122 summary => 'Return the GUID instead of the name',
123 schema => 'bool*',
124 },
125 name => {
126 summary => 'Return the name instead of the GUID (the default behavior)',
127 schema => 'bool*',
128 default => 1,
129 },
130 },
131 deps => {
132 prog => 'kactivities-cli',
133 },
134 args_rels => {
135 choose_one => [qw/guid name/],
136 },
137 };
138 0 ▶sub get_current_kde_activity {
Subroutine get_current_kde_activity: not called CC=5 SCAR=34.0
Pod documented
139 0 my %args = @_;
140 0 my $detail = delete($args{detail});
141 0 ▶ my $name = delete($args{name}) // 1;
Condition0%0/2
delete $args{"name"} // 1
l!l
00
MC/DC0%0/1
delete $args{"name"} // 1
delete $args{"name"}
Truth table0%0/2
A
A= delete $args{"name"}
Aresult
11
00
142 0 my $guid = delete($args{guid});
143  
144 0 my $res = _list_kde_activities('current', %args, detail=>1);
145  
146 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
147 0 ▶ if ($detail) { [200, "OK", $res->[2][0]] }
Branch: 2 branches
BranchTrueFalseTotal
if ($detail) { } 0 0 0
elsif ($guid) { } 0 0 0
148 0 elsif ($guid) { [200, "OK", $res->[2][0]{guid}] }
149 0 else { [200, "OK", $res->[2][0]{name}] }
150 }
151  
152 0 ▶my $_comp_kde_activity_name = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
153 0 require Complete::Util;
154  
155 0 my %args = @_;
156 0 my $word = $args{word};
157  
158 0 my $res = list_kde_activities(detail => 1);
159 0 ▶ return unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
160  
161 0 Complete::Util::complete_array_elem(word => $word, array=>[ map { $_->{name} } @{$res->[2] }]);
162 1 };
163  
164 $SPEC{set_current_kde_activity} = {
165 1 v => 1.1,
166 summary => 'Set KDE current activity',
167 description => <<'MARKDOWN',
168  
169 Features:
170 - Specifying activity by name (kactivities-cli wants GUID)
171 - Tab completion
172  
173 MARKDOWN
174 args => {
175 name => {
176 schema => 'str*',
177 req => 1,
178 pos => 0,
179 completion => $_comp_kde_activity_name,
180 },
181 # TODO: guid as alternative way to specify the activity
182 },
183 deps => {
184 prog => 'qdbus',
185 },
186 };
187 0 ▶sub set_current_kde_activity {
Subroutine set_current_kde_activity: not called CC=7 SCAR=40.3
Pod documented
188 0 my %args = @_;
189 0 ▶ defined(my $name = $args{name}) or return [400, "Please specify name"];
Branch: 1 branch
BranchTrueFalseTotal
defined(my $name = $args{"name"}) or return [400, "Please specify name"] 0 0 0
190  
191 0 my $res = list_kde_activities(detail => 1);
192 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
193  
194 0 my $guid;
195 0 for my $row (@{ $res->[2] }) {
196 0 ▶ do { $guid = $row->{guid}; last } if $row->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"name"} eq $name) 0 0 0
197 }
198 0 ▶ return [404, "Cannot find activity named '$name'"] unless $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless $guid 0 0 0
199  
200 0 system({capture_stdout => \my $dummy}, "qdbus", "org.kde.ActivityManager", "/ActivityManager/Activities", "SetCurrentActivity", $guid);
201 0 ▶ return [500, "Can't run qdbus"] if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
202  
203 0 [200];
204 }
205  
206 1 1;
207 # ABSTRACT: Utilities related to KDE Activities
208  
209 __END__
↑ Summary