Integration of trigonmetric function $(a\cos x + b\sin x + c)/(1 - d\cos x)^{2}$ 
Problem: Prove that the indefinite integral $$\int \frac{a\cos x + b\sin x + c}{(1 - d\cos x)^{2}}\,dx$$ is rational function of $\sin x, \cos x$ if and only if $ad + c = 0$.

My Try: Looking at the problem it is obvious that if $a = c = 0$ then the integral is a rational function of $\sin x, \cos x$. Another hope is that the integral if rational should be of the form $(A\cos x + B\sin x)/(1 - d\cos x)$ so that $$\left(\frac{A\cos x + B\sin x}{1 - d\cos x}\right)' = \frac{(B\cos x - A\sin x - Bd)}{(1 - d\cos x)^{2}}$$ Comparing we get $B = a, A = -b, c = -Bd$ so that $ad + c = 0$.
But the problem I see in this intuitive approach is to show that this is the only possible form of the integral if it is supposed to be a rational function of $\sin x, \cos x$. Any helpful ideas or a proper solution is welcome.
Source of the problem: A Course of Pure Mathematics, G. H. Hardy, 10th ed., Page 284, Problem 64.
 A: Let
$$E=\frac{b}{d(d\cos x-1)}-\frac{2\left(cd+a\right)}{d^2-1} \cdot\frac{\tan \left(\frac x2\right)}{(d+1)\tan^2 \left(\frac x2\right)-d+1}
$$
$$F=\frac{ad+c}{(d^2-1)^{3/2}}\;\log \left|\frac{\sqrt{\frac{d+1}{d-1}}\tan\left(\frac x2\right)+1}{\sqrt{\frac{d+1}{d-1}}\tan\left(\frac x2\right)-1}\right|$$
Then, if $d^2>1$,
$$\int {{{a\,\cos x+b\,\sin x+c}\over{\left(1-d\,\cos x\right)^2}}
 }{\;dx}=E+F$$
And if $d^2<1$, you have another form with arctan, with the same factor $ad+c$ (E is the same):
$$F=\frac{ad+c}{(1-d^2)^{3/2}}\;\arctan \left(\sqrt{\frac{d+1}{d-1}}\tan\left(\frac x2\right)\right)$$
Notice that $\tan \frac x2=\frac{\sin x}{1+\cos x}$.
Thus, the result is a rational function of sin, cos iff $ad+c=0$, but this comes from F.
By the way, if $d=0$, you get trivially a rational function of sin, cos whatever the values of a, b, c.
And if $d=1$, your whole primitive is
$$\frac a6 \cot^3 \left(\frac x2\right)\left(3\tan^2 \left(\frac x2\right)-1\right)-\frac c6 \cot^3 \left(\frac x2\right)\left(3\tan^2 \left(\frac x2\right)+1\right)+\frac{b}{\cos (x) - 1}$$
Thus it's again a rational function of sin, cos whatever the values of a, b, c.
The original statement should be "... iff $ad+c=0$ or $d=0$ or $d=1$".
Also, having written the primitive this way is not really a proof that it cannot be written with a rational function of sin, cos. I mean, it doesn't prove the log or arctan cannot be written otherwise. It looks obvious, but it's not proved as is.
